CS 342 Lecture -*- Outline -*- * Procedures, a built-in, first-class type of Scheme ** lambda makes procedures for all symbols x, for all expressions e1, e2: ((lambda (x) e1) e2) =def= value of e1, in environment where x denotes value of e2 (applicative order) x (the formal) is just a place-holder, (compare integrals) can change it and all references uniformly examples: ---------- ((lambda (x) x) 'y) ; Value: Y ((lambda (x) (car x)) '(a b c)) ; Value: A ------- note: above is same as "car" (eta conversion) (lambda (x) (e1 x)) =def= e1, if x does not occur free in e1 ---------- ((lambda (x y) 0) 'a 'b) ; Value: 0 ((lambda () 5)) ; no arguments ; Value: 5 ---------- ** Procedures first-class in LISP: (lambda (x) e1) denotes functional abstraction of e1. *** examples --------------- (lambda (x) x) ; identity function ; Value: #[COMPOUND-PROCEDURE #x1A0418] (define id (lambda (x) x)) (id 1) ; Value: 1 ((lambda (y) y) 3) ; Value: 3 (define one 1) (define inc (lambda (x) (+ one x))) (inc 2) ; Value: 3 (set! one 2) ; mutation of global environment! (inc 2) ; Value: 4 ; closes over bindings, not values! ------------------- *** environment model *draw pictures: ______________________ global env -> | one: 1 | | | | inc: . | |______|_____________| | ^ v | _________|___ | . | * | |_____|_____| | v parameters: x body : (+ one x) **Quiz on the above: ---------- Consider the following definitions: (define (compose f g) (lambda (x) (f (g x)))) (define (curry f) (lambda (x) (lambda (y) (f x y)))) What are the values of the following expressions: 1. ((compose car cdr) '(a b c)) 2. ((compose car (compose cdr cdr)) '(a b c)) 3. (((curry +) 1) 2) ---------- Answers: 1. B 2. C 3. 3 *** functionals see quiz above for curry and compose **** abstraction of common patterns ------------------ (define (sum lst) (cond ((null? lst) 0) (else (+ (car lst) (sum (cdr lst)))))) (define (double lst) ; turns '(a b c) into (a a b b c c) (cond ((null? lst) '()) (else (append (list (car lst) (car lst)) (double (cdr lst)))))) (define (reduction-form combiner c-id car-fun lst) (cond ((null? lst) c-id) (else (combiner (car-fun (car lst)) (reduction-form combiner c-id car-fun (cdr lst)))))) ------------------ **** higher-order programming ------------------- ;; combinators: (define B (curry compose)) (define (W f) (lambda (x) ((f x) x))) (define twice (W B)) (define I (lambda (x) x)) (define (K c) (lambda (x) c)) (define (S f g) (lambda (x) (f x (g x)))) (define CS (curry S)) ;; applicative order Y combinator: (define (Y M) ((lambda (future) (M (lambda (arg) ((future future) arg)))) (lambda (future) (M (lambda (arg) ((future future) arg)))))) ------------------- *** functions as data infinite sets represented by predicates -------------------- (define the-empty-set (lambda (E) #f)) (define (such-that P) P) (define (singleton E1) (lambda (E) (equal? E1 E))) (define (union S1 S2) (lambda (E) (or (S1 E) (S2 E)))) (define (intersect S1 S2) (lambda (E) (and (S1 E) (S2 E)))) (define (complement S) (lambda (E) (not (S E)))) (define (has S E) (S E)) --------------------