Functional Programming - Boise State...

Post on 17-Aug-2020

0 views 0 download

Transcript of Functional Programming - Boise State...

Functional Programming

Alark Joshi

Some slides are based on the slides at http://courses.cs.vt.edu/~cs3304/Spring02/lectures/lect04.pdf

Thursday, November 15, 12

Functional Programs

• Functional programs are made up of functions applied to data

• We write expressions rather than commands that need to be executed

• Pure functional languages have no side effects - What are side effects?

Thursday, November 15, 12

Functional Programming in ML• ML was developed in Edinburgh in late

1970’s by Milner, Gordon and Wadsworth

• Meta-Language for automated theorem proving

• ML is not a pure language

• Reference variables, commands, I/O

Thursday, November 15, 12

ML Characteristics

Thursday, November 15, 12

ML Characteristics

• Functions are first class citizens

Thursday, November 15, 12

ML Characteristics

• Functions are first class citizens

• Statically scoped

Thursday, November 15, 12

ML Characteristics

• Functions are first class citizens

• Statically scoped

• Static typing via type inference

Thursday, November 15, 12

ML Characteristics

• Functions are first class citizens

• Statically scoped

• Static typing via type inference

• Polymorphic types

Thursday, November 15, 12

ML Characteristics

• Functions are first class citizens

• Statically scoped

• Static typing via type inference

• Polymorphic types

• Exception handling

Thursday, November 15, 12

ML Characteristics

• Functions are first class citizens

• Statically scoped

• Static typing via type inference

• Polymorphic types

• Exception handling

• Garbage collection

Thursday, November 15, 12

Using the ML Interpreter

Thursday, November 15, 12

Using the ML Interpreter

• Type sml

Thursday, November 15, 12

Using the ML Interpreter

• Type sml

• Hyphen (-) is the prompt, = waiting/incomplete

Thursday, November 15, 12

Using the ML Interpreter

• Type sml

• Hyphen (-) is the prompt, = waiting/incomplete

• Type at the prompt or write a program in a file

Thursday, November 15, 12

Using the ML Interpreter

• Type sml

• Hyphen (-) is the prompt, = waiting/incomplete

• Type at the prompt or write a program in a file

• Type use “file.sml” to load definition from the file

Thursday, November 15, 12

Using the ML Interpreter

• Type sml

• Hyphen (-) is the prompt, = waiting/incomplete

• Type at the prompt or write a program in a file

• Type use “file.sml” to load definition from the file

• End session by typing Ctrl-D.

Thursday, November 15, 12

Expressions

• Expression evaluation:

- 3;

val it = 3; int

- 23 - 6;

val it = 17; int

Name it refers to the last computed value

Thursday, November 15, 12

Constants

• In ML, we name values rather than have variables

- val pi = 3.14159;

val pi = 3.14159 : real

- val r = 2.0;

val r = 2.0 : real

- val area = pi * r * r;

val area = 12.56636 : real

Thursday, November 15, 12

Functions

• Syntax: fun name arg = expression

• Example:

- fun area (r) = pi * r * r;

val area = fn: real->real

• For a single parameter, the () are optional. Area could be invoked as area 3

Thursday, November 15, 12

Functions

• A function can also be written as a value

- val area = fn r => pi *r *r

val area = fn: real -> real

- area 2.0; or area(2.0);

val it = 12.56636 : real

Thursday, November 15, 12

Environment

• pi define outside of area

val pi = 3.14159;

fun area(r) = pi*r*r;

• Environment of function determines value

Thursday, November 15, 12

Environment

• What happens if we change the value of pi?

- area 1.1;

val it = 3.8013239 : real

- val pi = 2000;

val pi = 2000 : int

- area 1.1;

val it = 3.8013239 : real

Thursday, November 15, 12

Structured Data Types

• Tuples - ordered collection of values

• Records - collection of named values

• Lists - list of values of homogeneous types

Thursday, November 15, 12

Thursday, November 15, 12