functional languages
play

Functional Languages Languages: LISP, Scheme (dialect of LISP from - PDF document

7/6/2015 Functional Programming Paradigm The program is a collection of functions A function computes and returns a value No side effects (i.e., no changes to state) No program variables whose values change Basically, no


  1. 7/6/2015 Functional Programming Paradigm • The program is a collection of functions – A function computes and returns a value – No side ‐ effects (i.e., no changes to state) – No program variables whose values change • Basically, no assignments Functional Languages • Languages: LISP, Scheme (dialect of LISP from MIT, mid ‐ 70s), ML, Haskell, … • Functions as first ‐ class entities Chapter 10 – A function can be a parameter of another function – A function can be the return value of another function – A function could be an element of a data structure – A function can be created at run time 2 Outline Data Objects in Scheme • Language elements: • Atoms – Atoms and lists – Numeric constants: 5, 20, ‐ 100, 2.788 • Evaluating expressions – Boolean constants: #t (true) and #f (false) – Function application – String constants: “hi there” – Quoting an expression – Character constants: #\a – Conditionals – Symbols: f, x, +, *, null?, set! – Defining functions • Roughly speaking, equivalent to identifiers in imperative • Examples languages • S ‐ expressions – Empty list: ( ) • Function call semantics & higher ‐ order functions • Lists • More examples and features – (e 1 e 2 … e n ) where e i is an atom or list 3 4 Examples of Lists Lists • (A B C) • List elements can be atoms or other lists • ((A B) C) – ( ( 3 4 ) 5 ( 6 ) ) is a list with 3 elements – Thus, lists are heterogeneous: the elements do not • ((3) (4) 5) have to be of the same type • (A B (C D)) • Empty list ( ) ‐ has zero elements • ((A)) – Operations car and cdr are not defined for an empty • () list – run ‐ time error • (()) 5 6 1

  2. 7/6/2015 Lists Outline • Language elements: • car for a list produces the first element of the list – Atoms and lists (the list head) • Evaluating expressions – e.g. for ( ( A B ) ( C D ) E ) will produce ( A B ) – Function application • cdr produces the tail of the list: a list containing – Quoting an expression all elements except the first – Conditionals – e.g. for ( ( A B ) ( C D ) E ) will produce ( ( C D ) E ) – Defining functions • cons adds to the beginning of the list • Examples – cons of A and ( B C ) is ( A B C ) • S ‐ expressions – e.g., cons of car of x and cdr of x is x • Function call semantics & higher ‐ order functions • More examples and features 7 8 Data vs. Code Using Scheme • Interpreter for an imperative language: the input • Read : you enter an expression is code+data, the output is data (values) • Eval : the interpreter evaluates the expression • Everything in Scheme is an S ‐ expression • Print : the interpreter prints the resulting value – The “program” we are executing is an S ‐ expression • stdlinux: at the prompt, type scheme48 – The intermediate values and the output values of the > type your expression here program are also S ‐ expressions the interpreter prints the value here • Data and code are really the same thing > ,help • Example: an expression that represents function > ,exit application (i.e., function call) is a list (f p1 p2 …) – f is an S ‐ expression representing the function we are calling; p1 is an S ‐ expression representing the first actual parameter, etc. 9 10 Evaluation of Atoms Outline • Numeric constants, string constants, and • Language elements: character constants evaluate to themselves – Atoms and lists > 4.5 > #t • Evaluating expressions 4.5 #t – Function application > “This is a string” > #f – Quoting an expression “This is a string” #f – Conditionals • Symbols do not have values to start with – Defining functions – They may get “bound” to values, as discussed later • Examples > x Error: undefined variable x • S ‐ expressions • The empty list ( ) does not have a defined value • Function call semantics & higher ‐ order functions • More examples and features 11 12 2

  3. 7/6/2015 Function Application Outline • Language elements: • (+ 5 6) – Atoms and lists – This S ‐ expression is a “program”; here + is a symbol • Evaluating expressions “bound” to the built ‐ in function for addition – Function application – The evaluation by the interpreter produces the S ‐ – Quoting an expression expression 11 – Conditionals • Function application: (f p1 p2 …) – Defining functions – The interpreter evaluates S ‐ expressions f, p1, p2, etc. • Examples – The interpreter invokes the resulting function on the • S ‐ expressions resulting values • Function call semantics & higher ‐ order functions • More examples and features 13 14 Quoting an Expression Examples > (+ (+ 3 5) (car (7 8))) • When the interpreter sees a non ‐ atom, it tries to Errors evaluate it as if it were a function call 1> Ctrl ‐ D – But for (5 6), what does it mean? > (+ (+ 3 5) (car '(7 8))) • “Error: attempt to call a non ‐ procedure” 15 > (car (7 10)) • We can tell the interpreter to evaluate an Errors expression to itself 1> (car '(7 10)) – (quote (5 6)) or simply '(5 6) 7 1> (+ (car '(7 10)) (cdr '(7 10))) – Evaluates to the S ‐ expression (5 6) Errors – The resulting expression is printed by the Scheme 2> (+ (car '(7 10)) (car (cdr '(7 10)))) interpreter as '(5 6) 17 15 16 More Examples More Examples > (equal? #t #f) > (equal? '() #f) > (cons (car '(7 10)) (cdr '(7 10))) #f #f '(7 10) > (equal? #t #t) > (equal? (+ 7 5) (+ 5 7)) > a > 'a > (car '(A B)) #t #t Error 'a 'a > (equal? (cons 'a '(b)) '(a b)) > (cdr '(A B)) > (cons 'a '(b)) > (cons 'a 'b) #t '(b) '(a b) '(a . b) > (pair? '(7 . 10)) > (pair? 7) > (pair? '()) #t #f #f > (null? '()) > (null? #f) > (null? '(b)) #t #f #f 17 18 3

  4. 7/6/2015 More Examples Outline • Language elements: > (even? 7) > (even? 8) #f #t – Atoms, S ‐ expressions, lists > (even? (+ 7 7)) > (even 7) > (even? 'a) • Evaluating expressions #t Error Error – Function application > (= 5 6) > (< 5 6) > (> 5 6) – Quoting an expression #f #t #f – Conditionals > (= 4.5 4.5 4.5) > (= 4.5 4.5 4.7) #t #f – Defining functions > (= 'a 'b) • Examples Error • Function call semantics & higher ‐ order functions • More examples and features 19 20 Conditional Expressions Function Definition • ( if b e 1 e 2 ) > (define (double x) (+ x x)) ; no values returned – Evaluate b. If the value is not #f, evaluate e 1 and this is the value to the expression > (double 7) > (double 4.4) > (double '(7)) – If b evaluates to #f, evaluate e 2 and this is the value of 14 8.8 Error the expression • ( cond (b 1 e 1 ) (b 2 e 2 ) … (b n e n )) > (define (mydiff x y) (cond ((= x y) #f) (#t #t))) – Evaluate b 1 . If not #f, evaluate e 1 and use its value. If ; no values returned b 1 evaluates to #f, evaluate b 2 , etc. – If all b evaluate to #f: unspecified value for the > (mydiff 4 5) > (mydiff 4 4) > (mydiff '(4) '(4)) expression; so, we often have #t as the last b #t #f ??? – Alternative form: ( cond (b 1 e 1 ) (b 2 e 2 ) … (else e n )) 21 22 Outline Member of a List? • Language elements: In text file mbr.ss create the following: ; this is a comment – Atoms, S ‐ expressions, lists ; (mbr x list): is x a member of the list? • Evaluating expressions (define (mbr x list) – Function application (cond – Quoting an expression ( (null? list) #f ) – Conditionals ( #t (cond – Defining functions ( (equal? x (car list)) #t ) • Examples ( #t (mbr x (cdr list)) ) ) ) ) • Function call semantics & higher ‐ order functions ) • More examples and features Or we could use just one “cond” … 23 24 4

  5. 7/6/2015 Member of a List? Union of Two Lists (define (uni s1 s2) In the interpreter: How about using ”if” (cond > (load “mbr.ss”) or ,load mbr.ss in mbr and uni? ( (null? s1) s2) mbr.ss ( (null? s2) s1) ; no values returned ( #t (cond > (mbr 4 '( 5 6 4 7)) ( (mbr (car s1) s2) (uni (cdr s1) s2)) #t ( #t (cons (car s1) (uni (cdr s1) s2))))))) > (uni '(4) '(2 3)) > (mbr 8 '(5 6 4 7)) '(4 2 3) #f > (uni '(3 10 12) '(20 10 12 45)) '(3 20 10 12 45) 25 26 Removing Duplicates Largest Number in a List ; x: a sorted list of numbers; remove duplicates ... ; max number in a non ‐ empty list of numbers (define (unique x) (define (maxlist L) (cond (cond ( (null? x) x ) ( (null? (cdr L)) (car L) ) ( (null? (cdr x)) x ) ( (> (car L) (maxlist (cdr L))) (car L) ) ( (equal? (car x) (cdr x)) (unique (cdr x)) ) ( #t (maxlist (cdr L)) ) ( #t (cons (car x) (unique (cdr x))) ) ) ) ) ) What is the running time as a function of list size? How > (unique '(2 2 3 4 4 5)) can we improve it? (2 2 3 4 4 5) ;??? 27 28 A Different Approach Outline • Language elements: ; max number in a non ‐ empty list of numbers – Atoms, S ‐ expressions, lists (define (maxlist L) (mymax (car L) (cdr L))) • Evaluating expressions (define (mymax x L) – Function application (cond – Quoting an expression ( (null? L) x ) – Conditionals ( (> x (car L)) (mymax x (cdr L)) ) – Defining functions ( #t (mymax (car L) (cdr L)) ) • Examples ) • S ‐ expressions ) • Function call semantics & higher ‐ order functions What is the running time as a function of list size? • More examples and features 29 30 5

Recommend


More recommend