SlideServe
Menu
Préférences
© SlideServe · v2026

PPL

jasmine-kaufman avatar
26 diapositives · 498 Views · Oct 31, 2014

PPL. Lecture 3 Slides by Yaron Gonen , based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban. Warm-up. Is this recursive or iterative? (define sum (lambda (n) (if (= n 0) 0 (+ (/ 1 (pow 2 n)) (sum (- n 1)))))).

PPL - slide 1
01 / 26PPL
1 / 17

Télécharger la présentation

PPL
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. PPL Lecture 3 Slides by YaronGonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

  2. Warm-up Is this recursive or iterative? (define sum (lambda (n) (if (= n 0) 0 (+ (/ 1 (pow 2 n)) (sum (- n 1))))))

  3. The Iterative Version (define sum-iter (lambda (n prod) (if (= n 0) prod (sum-iter (- n 1) (+ prod (/ 1 (pow 2 n)))))))

  4. Today: High-Order Procedures In functional programming (hence, in Scheme) procedures have a first class status: • Pass procedures as arguments • Create them at runtime • Help in creating local-variables

  5. Motivation Example: sum-integers ;Signature: sum-integers(a,b) ;Purpose: to compute the sum of integers in the interval [a,b]. ;Type: [Number*Number -> Number] ;Post-conditions: result = a + (a+1) + ... + b. ;Example: (sum-integers 1 5) should produce 15 (define sum-integers (lambda (a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b)))))

  6. Motivation Example: sum-cubes ;Signature: sum-cubes(a,b) ;Purpose: to compute the sum of cubic powers of ;integers in the interval [a,b]. ;Type: [Number*Number -> Number] ;Post-conditions: result = a^3 + (a+1)^3 + ... + b^3. ;Example: (sum-cubes 1 3) should produce 36 (define sum-cubes (lambda (a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b)))))

  7. Motivation Example: pi-sum (define pi-sum (lambda (a b) (if (> a b) 0 (+ (/ 1 (* a (+ a 2))) (pi-sum (+ a 4) b)))))

  8. Same Pattern (define <name> (lambda (a b) (if (> a b) 0 (+ (<term> a) (<name> (<next> a) b)))))

  9. Abstraction ;Signature: sum(term,a,next,b) ;Purpose: to compute the sum of terms, defined by <term> ;in predefined gaps, defined by <next>, in the interval [a,b]. ;Type: [[Num -> Num] * Num * [Num -> Num] * Num -> Num] ;Post-conditions: result = (term a) + (term (next a)) + ... (term n), ;where n = (next (next ...(next a))) =< b, ;(next n) > b. ;Example: (sum identity 1 add1 3) should produce 6, ;where ’identity’ is (lambda (x) x) (define sum (lambda (term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))))

  10. Using the abstracted form (define id (lambda(x) x)) (define add1 (lambda (x) (+ x 1))) (define pi-term (lambda (x) (/ 1 (* x (+ x 2))))) (define pi-next (lambda (x) (+ x 4))) (define sum-integers (lambda (a b) (sum id a add1 b))) (define pi-sum (lambda (a b) (sum pi-term a pi-next b)))

  11. Advantages • Code reuse • Easier maintenance, understanding, debugging… • General interface • Expresses a well-defined concept • Allows for further abstraction (next slide)

  12. Sequence Operations ;Signature: … ;Type: [[Number*Number -> Number]*Number*Number*Number -> Number] ;… ;Example: (sequence-operation * 1 3 5) is 60 ;Tests: (sequence-operation + 0 2 2) ==> 2 (define sequence-operation (lambda (operation start a b) (if (> a b) start (operation a (sequence-operation operation start (+ a 1) b)))))

  13. Anonymous Procedures • λ forms evaluated during computation (no define) • Useful in many cases. (define pi-sum (lambda (a b) (sum (lambda (x) (/ 1 (* x (+ x 2)))) a (lambda (x) (+ x 4)) b)))

  14. Anonymous Procedures • Disadvantage: careless use may cause the same λ to reevaluate: (define sum-squares-iter (lambda (n sum) (if (= n 0) sum (sum-squares-iter (- n 1) (+ sum ((lambda (x) (* x x)) n))))))

  15. Scope and Binding • In a λ form every parameter has • Binding (declaration) • Occurrence • Scope: lexical scoping • In nested λ, things are a little tricky. • An occurrence without binding is called free • define is also declaration. Its scope is universal.

  16. Scope and Binding (lambda (f a b dx) (* (sum f (+ a (/ dx 2.0)) (lambda (x) (+ x dx)) b) dx))

  17. Local Variables • Essential programming technique: mostly used for saving repeated computation. • Can we use scoping to define local variables? Yes we can!

  18. Local Variables Consider the function: It is useful to define 2 local variables: a = 1+xy b = 1-y

  19. Local Variables

  20. Local Variables (define f (lambda (x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y)) ))

  21. Let (define f (lambda ( x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b)))))

  22. Let (let ( (<var1> <exp1>) (<var2> <exp2>) ... (<varn> <expn>) ) <body> )

  23. Let vs. Anonymous Lambda Let Lambda (define f (lambda (x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y)) )) (define f (lambda ( x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b)))))

  24. Notes about Let • let provides variables (declaration and scope) • <expi> are in outer scope. • <body> is the scope. • Let variables are the binind.

  25. From Midterm 2008 • סמן את כל הבלוקים הלקסיקליים (scopes) בקטע הבא. • מהו הערך המוחזר? (let ((x 2)) (let ( (x 3) (y x) ) ((lambda (x y +) (+ x y)) (- x y) x *)))