Environment Model Examples Prof. Tony White April 2010 Ref:

Slides:



Advertisements
Similar presentations
And other languages….  Writing programs that write programs (cool!)  Often used to create domain-specific languages (DSL) You’ve all heard of at least.
Advertisements

(define (f x) (if (< x 0) (lambda (y) (- y x)) (lambda (y) (- x y)))) GE f: P1 para:x body:(if … )
© 2007 by S - Squared, Inc. All Rights Reserved.
AB 11 22 33 44 55 66 77 88 99 10  20  19  18  17  16  15  14  13  12  11  21  22  23  24  25  26  27  28.
PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
Exercise Exercise3.1 8 Exercise3.1 9 Exercise
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
מבוא מורחב 1 Lecture #13. מבוא מורחב 2 Multiple representations of data.
Exercise Exercise Exercise Exercise
HASKELL Presentation Programming Segment
Exercise Exercise Exercise Exercise
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
Exercise Exercise6.1 7 Exercise6.1 8 Exercise6.1 9.
Question TitleMean limit pcw-function71.43% limit abs from left53.57% squeeze thm57.14% limit at inf67.86% limit of tan^(-1)46.43% limit multiply by conjugate32.14%
1 Lecture 15: More about assignment and the Environment Model (EM)
מבוא מורחב 1 Lecture 3 Material in the textbook on Pages of 2nd Edition Sections to
Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.
Scheme Tutorial. Goals Combine several simple ideas into one compound idea to obtain complex ideas Bring two ideas together to obtain relations Seperate.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
5 or more raise the score 4 or less let it rest
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections ,
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion.
Principles Of Programming Languages Lecture 2 Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
1 Lecture 14: Assignment and the Environment Model (EM)
Bank Account Environment Model Examples Prof. Tony White April 2010.
160 as a product of its prime factors is 2 5 x 5 Use this information to show that 160 has 12 factors.
PPL Lecture 4 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Formulas. Demonstrate a 2 + 2ab + b 2 = (a + b) 2 Find the area of the big square by adding up the areas of the 2 squares and 2 rectangles: a 2 + ab +
Higher-Order Programming: Iterative computation (CTM Section 3
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
6.001 Jeopardy.
6.001 SICP Object Oriented Programming
Computing Square Roots
Higher-Order Programming: Iterative computation (CTM Section 3
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Original material by Eric Grimson
6.001 SICP Data abstractions
2.3 Representation Strategies for Data Types
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Square Roots & Cube Roots
Lecture 18 Infinite Streams and
Rational and irrational subgrouping
Lecture 18.
CS220 Programming Principles
10:00.
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
topics mutable data structures
6.001 SICP Variations on a Scheme
Lecture 14: The environment model (cont
Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages ,2.5.2 pages מבוא.
Today’s topics Abstractions Procedural Data
Lecture 13: Assignment and the Environment Model (EM)
1.5b Combining Transformations
Good programming practices
Presentation transcript:

Environment Model Examples Prof. Tony White April 2010 Ref:

Example 1 fooλ(1) foo’ x2 1. (define (foo x) (+ x 1)) 2. (foo 2) Π=3 Π=1

Example 2 let’ x1 y2 Π=3 1.(let ((x 1) 2. (y 2)) 3. (+ x y)) Π=4

Example 3 lambda’ x1 y2 Π=1 1. (lambda (x y) (+ x y)) 1 2) Π=2

Example 4-a sqrtfλ(1) incλ(3) Π=4 1.(define (sqrtf f) 2. (lambda (x) (sqrt (f x)))) 3.(define (inc n) (+ n 1)) 4.(define f1 (sqrtf inc)) 5.(f1 3)

Example 4-b sqrtfλ(1) incλ(3) f1λ(2) sqrtf’ fλ(3) Π=5 1.(define (sqrtf f) 2. (lambda (x) (sqrt (f x)))) 3.(define (inc n) (+ n 1)) 4.(define f1 (sqrtf inc)) 5.(f1 3)

Example 4-c sqrtfλ(1) incλ(3) f1λ(2) sqrtf’ fλ(3) Π=6 x3 f1’ Π=2 1.(define (sqrtf f) 2. (lambda (x) (sqrt (f x)))) 3.(define (inc n) (+ n 1)) 4.(define f1 (sqrtf inc)) 5.(f1 3)

Example 4-d sqrtfλ(1) incλ(3) f1λ(2) sqrtf’ fλ(3) Π=6 x3 f1’ Π=2 n3 f’ Π=3 1.(define (sqrtf f) 2. (lambda (x) (sqrt (f x)))) 3.(define (inc n) (+ n 1)) 4.(define f1 (sqrtf inc)) 5.(f1 3)

Example 5-a barλ(4) Π=5 let’ x1 y2 1.(define bar 2. (let ((x 1) 3. (y 2)) 4. (lambda (z) (+ x y z)))) 5.(bar 3)

Example 5-b barλ(4) Π=6 let’ x1 y2 z3 bar’ Π=4 1.(define bar 2. (let ((x 1) 3. (y 2)) 4. (lambda (z) (+ x y z)))) 5.(bar 3)

Example 6: Code 1.(define (foo z) 2. (let ((x z)) 3. (let ((y (+ x z))) 4. (lambda (sym) 5. (cond ((eq? sym 'x) x) 6. ((eq? sym 'y) y) 7. ((eq? sym 'bump-x) (set! x (+ x z))) 8. ((eq? sym 'bump-y) (set! y (+ y z))) 9. ((eq? sym 'reset-x) (set! x 0)) 10. ((eq? sym 'reset-y) (set! y 0))))))) 11. (define f (foo 10)) 12. (f 'x) 13. (f 'y) 14. (f 'bump-x) 15. ; Rest of code not shown

Example 6-a fooλ(1) fλ(3) Π=12 z10 x y20 foo’

Example 6-b fooλ(1) fλ(3) Π=13 z10 x y20 foo’ sym‘x Π=5

Example 6-c fooλ(1) fλ(3) Π=15 z10 x20 y foo’ sym‘bump-x Π=7

Example 6-d Rest of code associated with example 6 left as an exercise to the reader. It really is a set of variations on 6-b and 6-c.

Example 7: Code 1.(define (new-sqrt x) 2. (define (good-enough? guess) 3. (< (abs (- (square guess) x)) )) 4. (define (average x y) 5. (/ (+ x y) 2)) 6. (define (improve guess) 7. (average guess (/ x guess))) 8. (define (sqrt-iter guess) 9. (if (good-enough? guess) 10. guess 11. (sqrt-iter (improve guess)))) 12. (sqrt-iter 1.0)) 13. (new-sqrt 2.0)

Example 7-a new-sqrtλ( 1) Π=13

Example 7-b new-sqrtλ( 1) Π=14 new-sqrt’ x2.0 good-enough?λ( 2) averageλ( 4) improveλ( 6) sqrt-iterλ( 8) Π=12

Example 7-c new-sqrtλ( 1) Π=14 new-sqrt’ x2.0 good-enough?λ( 2) averageλ( 4) improveλ( 6) sqrt-iterλ( 8) Π=12 guess1.0 sqrt-iter’ Π=9

Example 8-a fooλ( 1) Π=5 1.(define (foo x) 2. (let ((y (lambda (w) (+ x w))) 3. (z (lambda (w) (* x w)))) 4. (y (z x)))) 5.(foo 10)

Example 8-b fooλ( 1) Π=6 foo’ x10 Π=2 1.(define (foo x) 2. (let ((y (lambda (w) (+ x w))) 3. (z (lambda (w) (* x w)))) 4. (y (z x)))) 5.(foo 10)

Example 8-c fooλ( 1) Π=6 foo’ x10 Π=2 let’ yλ( 2) zλ( 3) Π=4 1.(define (foo x) 2. (let ((y (lambda (w) (+ x w))) 3. (z (lambda (w) (* x w)))) 4. (y (z x)))) 5.(foo 10)