Functional languages (e.g. Scheme, ML)

31
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined using the keyword lambda: (lambda (<parameters>) <body>) Syntax of Scheme is very simple: primitive expressions numbers, such as 17 symbols, such as ’fred names, such as fred complex expressions function applications, such as (+ 3 2) or (addOne x), consist of a function and arguments for the function. special forms, such as (define x y), are evaluated in special ways. ML is a functional language. ML is based on lambda calculus. lambda abstraction = function definition In ML, a function is (typically) defined using the fun keyword: fun <name> <param> = <body> Syntax of ML is familiar: block-structured syntax infix operators for +, *, etc function names appear before argument lists, not inside parentheses

description

Functional languages (e.g. Scheme, ML). Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined using the keyword lambda: (lambda () ) Syntax of Scheme is very simple: - PowerPoint PPT Presentation

Transcript of Functional languages (e.g. Scheme, ML)

Page 1: Functional languages (e.g. Scheme, ML)

1

Functional languages(e.g. Scheme, ML)

• Scheme is a functional language.• Scheme is based on lambda

calculus.• lambda abstraction = function

definition• In Scheme, a function is defined

using the keyword lambda: (lambda (<parameters>) <body>)

• Syntax of Scheme is very simple:• primitive expressions

• numbers, such as 17• symbols, such as ’fred• names, such as fred

• complex expressions• function applications, such as (+

3 2) or (addOne x), consist of a function and arguments for the function.

• special forms, such as (define x y), are evaluated in special ways.

• ML is a functional language.• ML is based on lambda calculus.• lambda abstraction = function

definition• In ML, a function is (typically)

defined using the fun keyword: fun <name> <param> = <body>

• Syntax of ML is familiar:• block-structured syntax• infix operators for +, *, etc• function names appear before

argument lists, not inside parentheses

Page 2: Functional languages (e.g. Scheme, ML)

2

Evaluation(Scheme and ML share model – focus on Scheme)

• Numbers are interpreted in base 10.• Names are looked up in an environment, starting with the current

environment, following static links, until the name is found. If it is not found, an error occurs.

• (define <var> <expr>) is evaluated by first evaluating <expr>, and binding, in the current environment, the name <var> to that value.

• (lambda …) is evaluated by creating an appropriate closure (sometimes called a procedure). The closure’s environment link refers to the environment which was active when the closure was created.

• All members of an application are evaluated, after which the first is applied to the rest.

• A procedure application is evaluated by creating an environment to bind the parameters of the procedure to its arguments, and then evaluating the body relative to this new environment. The static link of the environment refers to the same environment as the closure, whereas the dynamic link refers to the environment that was active when the procedure was applied.

Page 3: Functional languages (e.g. Scheme, ML)

3

Examples> 1212> (define x 12)> x12> (define addOne (lambda (x) (+ x 1)))> (addOne x)13> (define add (lambda (x y) (+ x y)))> (add 3 x)15> (define adder (lambda (x) (lambda (y) (+ x y))))> (define add3 (adder 3))> (add3 x)15> (define add7 (adder 7))> (add7 x)19

- 12val it=12:int- val x=12;val x=12:int- xval it=12:int- fun addOne x = x+1;val addOne=fn:int->int- addOne xval it=13:int- fun add (x,y) = x+y;val add=fn:int*int->int;- add(3,x);val it=15:int- fun adder x y = x+y;val adder=fn:int->int->int- val add3 = adder 3;val add3=fn:int->int- add3 x;val it=15:int;- val add7 = adder 7;val add7=fn:int->int- add7 x;val it=19:int;

Page 4: Functional languages (e.g. Scheme, ML)

4

Scheme

• Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing problems.

• Many ideas in design patterns have their roots in functional programming (e.g. strategy allows us to treat methods as first-class, a decorator acts like a function which maps a function to a function – think of the stream decorators in Java).

• With mutation (the ability to change a name-value binding in an environment) sophisticated systems can be built quite compactly.

Page 5: Functional languages (e.g. Scheme, ML)

5

Review of evaluation model

• Numbers evaluation to “themselves” (a character string representing a number evaluates to its base 10 numeric representation).

• For example:> 213 if you ask Scheme to evaluate a

number

213 you get the number as a result

Page 6: Functional languages (e.g. Scheme, ML)

6

Evaluating names

• A name is evaluated by looking it up. Lookup starts in the current environment, and continues along the chain of statically-linked environments until either a binding is found (in which case the corresponding value is returned) or it isn’t (in which case an error occurs).

• For example (assuming no binding for x exists yet):> xError: reference to undefined identifier: x> (define x 12)> x12>

Page 7: Functional languages (e.g. Scheme, ML)

7

Primitives• Primitives, such as + and -, are ordinary names with name-value

bindings established at start-up in the primitive environment, the base environment for evaluation (base in the sense that it’s static link is null).

• We can use + wherever we need a function.• For example:

> (define applyOperator (lambda (op) (op 3 4)))> (applyOperator +)7> (applyOperator -)-1>

• We can also rebind the names of primitives (not a good idea in general, but this shows that primitives are not treated differently from other names in the language).

> (+ 3 4) name “+” refers to addition function7> (define + -) name “+” now refers to subtraction

function> (+ 3 4)-1

Page 8: Functional languages (e.g. Scheme, ML)

8

lambda forms

• A lambda form (lambda abstraction) defines a function in Scheme.

• Informally, the syntax is:(lambda (<parameters>) <body>)

• When a lambda form is evaluated a closure results (which is printed as #<procedure:name>).

• For example:> (define addOne (lambda (p) (+ p 1)))> addOne#<procedure:addOne>>

Page 9: Functional languages (e.g. Scheme, ML)

9

Function applications

• A function application is evaluated by first evaluating each expression inside the application, then applying the function to its arguments.

• This is accomplished by first creating a new environment in which the function’s parameters are bound to its arguments, and then evaluating the function’s body with respect to the new environment (which is now the current environment).

Page 10: Functional languages (e.g. Scheme, ML)

10

Evaluating (addOne 4)

+ <proc>

addOne

primitive env

user env

-cons

<proc><proc>

p

(+ p 1) p 4

(+ p 1)

Page 11: Functional languages (e.g. Scheme, ML)

11

Local bindings

• In a language like C or Java we can create local bindings inside a function/method by defining local variables:int mystery(int x, int y, int z) { int a = func1(x,y); int b = func2(y,z); <body>}

• Local bindings can be created using a let-form:(define mystery (lambda (x y z) (let ((a (func1 x y)) (b (func2 y z))) <body>)

• Let-form is just syntactic sugar for a procedure application; the above let-form is equivalent to:

((lambda (a b) <body>) (func1 x y) (func2 y z))

Page 12: Functional languages (e.g. Scheme, ML)

12

Evaluating(define foo (let ((a 1) (b 2)) (lambda (c) (+ a b c))))

+ <proc>

addOnefoo

primitive env

user env

-cons

<proc><proc>

a b

(lambda (c) (+ a b c))

a 1b 2

(lambda (c) (+ a b c))

c

(+ a b c)

Page 13: Functional languages (e.g. Scheme, ML)

13

Mutation

• set! allows an existing name-value binding to be changed.

Page 14: Functional languages (e.g. Scheme, ML)

14

Structuring data

• So far we’ve seen two types of values that we can bind to names:– numbers– functions

• two others are:– symbols– pairs

Page 15: Functional languages (e.g. Scheme, ML)

15

Symbols

• A symbol is an unevaluated name.• Consider the following interaction:

> (define x 12)> x12

• The x in the define is not evaluated. It is a symbol.

• The x in the second line is evaluated by looking up the value it is bound to in the current environment.

Page 16: Functional languages (e.g. Scheme, ML)

16

Using symbols

• We can bind a name to a symbolic value by quoting the symbol – quoting prevents evaluation:> (define fifi 'poodle)

> fifi

poodle

• The quote mark (') is syntactic sugar for the form (quote …); the above is equivalent to:> (define fifi (quote poodle))

Page 17: Functional languages (e.g. Scheme, ML)

17

Pairs

• cons is a primitive function which builds a pair.

• A pair has two members.

• (cons 'a 'b) builds a pair– whose first member is the symbol a– whose second member is the symbol b

• cons is therefore a pair constructor.

• Graphically:ba

Page 18: Functional languages (e.g. Scheme, ML)

18

accessors

• car returns the first member of a pair

• cdr returns the second member of a pair

> (define myPair (cons 'a 'b))

> (car myPair)

a

> (cdr myPair)

b

Page 19: Functional languages (e.g. Scheme, ML)

19

How is a pair displayed?

> (define myPair (cons 'a 'b))

> myPair

(a . b)

• Pairs are printed with a dot (.) separating the first (left) and second (right) components.

Page 20: Functional languages (e.g. Scheme, ML)

20

The REPL

• When interacting with a Scheme system, a Read-Eval-Print Loop (REPL) reads what you type, evaluates it, prints the resulting value, and loops back to repeat.

• The reader and printer parts of the REPL handle syntactic sugar (like converting 'a into (quote a)) and also printing things like lists.

Page 21: Functional languages (e.g. Scheme, ML)

21

Lists

• What is a list?

• A recursive definition:– the empty list () is a list– a pair whose cdr is a list is a list

• We can build a list using cons and ():> (define myList (cons 'a '()))

> myList

(a)

Page 22: Functional languages (e.g. Scheme, ML)

22

Wait a minute!!

> (define myPair (cons 'a 'b))

> myPair

(a . b)

> (define myList (cons 'a '())

> myList

(a)

Page 23: Functional languages (e.g. Scheme, ML)

23

What's going on?

• Why did myList print as (a) rather than as (a . ())?

• In fact, they are entirely equivalent: the REPL strikes again. The printer formats pairs which cdr is a pair in a special way to make lists easier to read.

Page 24: Functional languages (e.g. Scheme, ML)

24

Printing lists

• (a . (b . (c . (d . ())))) is printed as (a b c d)• Graphically the structure is:

• This structure can be built in a variety of ways, some of which are shown on the next slide.

a b c d

Page 25: Functional languages (e.g. Scheme, ML)

25

Choices…choices

• (cons 'a (cons 'b (cons 'c (cons 'd '()))))

• (list 'a 'b 'c 'd)

• '(a b c d)

• '(a . (b c d))

• (cons 'a (list 'b 'c 'd))

and so on!

Page 26: Functional languages (e.g. Scheme, ML)

26

Association lists

• An association list is a list of pairs.

• Used to implement a look-up table of key-value pairs.

• The primitive assoc is used to search an association list, given a key.

• Example on next slide.

Page 27: Functional languages (e.g. Scheme, ML)

27

> (define aList '((fifi . poodle)

(fido . bulldog) (clifford . bigRed)

(lassie . collie)))

> (assoc 'fido aList)

(fido . bulldog)

> (assoc 'rufus aList)

#f

Page 28: Functional languages (e.g. Scheme, ML)

28

Mutating pair components• set-car! mutates the value of the car of a pair• set-cdr! mutates the value of the cdr of a pair

> (define myPair (cons 'a 'b))> myPair(a . b)> (set-car! myPair 'fred)> myPair(fred . b)> (set-cdr! myPair 'wilma)> myPair(fred . wilma)

Page 29: Functional languages (e.g. Scheme, ML)

29

Lists in other languages

• Lists in ML

• Lists in Prolog

Page 30: Functional languages (e.g. Scheme, ML)

30

Homework issues

• What is basic algorithm?

Page 31: Functional languages (e.g. Scheme, ML)

31

Homework issues

• What is basic algorithm?– while file is not empty

• read in a line• if the line is an odd line,

– then write line to output file– else don’t