6.001 SICP 1 6.001 SICP – September 17 6001-Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D512 6.001 web page:

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Advertisements

Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
6.001 SICP SICP – September Introduction Trevor Darrell 32-D web page:
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
6.001 SICP – September Introduction
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
6.001 SICP SICP – September Types and HOPs Trevor Darrell 32-D512 Office Hour: W web page:
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק: ינון פלד Book: Structure.
מבוא מורחב 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.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
1 Lecture 14: Assignment and the Environment Model (EM)
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
From Lambda Calculus to LISP Functional Programming Academic Year Alessandro Cimatti
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
CS314 – Section 5 Recitation 10
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Env. Model Implementation
6.001 SICP Stacks and recursion
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.
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming
This Lecture Substitution model
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
6.001 SICP Environment model
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Environment model
6.001 SICP Explicit-control evaluator
Material in the textbook Sections to 1.2.1
The structure of programming
Lecture 2 מבוא מורחב.
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 13: Assignment and the Environment Model (EM)
Introduction to the Lab
This Lecture Substitution model
Lecture 2 מבוא מורחב.
Rules of evaluation The value of a number is itself.
Lecture 6 - Recursion.
Presentation transcript:

6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page: section web page: Review –Syntax –Evaluation –Procedures –Special Forms Scheme processes Substitution model Recursion Iteration

6.001 SICP 2 Review-Syntax Things that make up scheme programs: primitive types 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special forms (define pi 3.14) (lambda … ) (if … ) Note that special forms are not combinations, even though they are syntactically similar. A combination requires that the first subexpression be a procedure.

6.001 SICP 3 Review-Evaluation Rules A numeral, string or boolean evaluates to itself (number, string, #t, #f). A name evaluates to the value bound to that name in the environment A combination is evaluated as follows: 1.Evaluate the subexpressions in any order 2.Apply the value of the operator subexpression to the values of the remaining subexpressions. define and lambda special forms are special: 1.A define associates the value of the second argument with the name given in the first argument 2.A lambda expression evaluates to a procedure object that stores both the parameter list and procedure body

6.001 SICP 4 Review-Special forms (if ) (and … ) (or … ) (cond ( … ) ( … )... ( … ) (else … )) (begin )

6.001 SICP 5 Drill : AND / OR from COND / IF (and a b c) === (if a (if b (if c c #f) #f) (cond (a (cond (b (cond (c c) (else #f))) (else #f)) even better… (cond ((not a) #f) ((not b) #f) ((not c) #f) (else c))

6.001 SICP 6 Drill : AND / OR from COND / IF (or a b c) === (cond (a a) (b b) (c c) (else #f)) (if a a (if b b (if c c #f)))

6.001 SICP 7 Review-Procedures Procedures capture a common form… (* 3 3), (* 4 4), (* 96 96), … (define square (lambda (y) (* y y))) name of proc. name of part of pattern that varies or, (define (square y) (* y y)) To apply a procedure given a procedure and argument values: If it is a primitive procedure, “just do it”. If is a compound procedure…

6.001 SICP 8 Substitution model To apply a compound procedure to its arguments: evaluate the body with each formal parameter replaced by the corresponding argument Some terminology: in the definition (define (square i) (* i i) ) i is the formal parameter (* i i) is the body in the application (square 3) 3 is the argument

6.001 SICP 9 Substitution example (define (square i) (* i i)) (define (sum-of-squares i j) (+ (square i) (square j))) (sum-of-squares 3 4) … (+ (square 3) (square 4)) (+ (* 3 3) (* 4 4)) (+ (* 3 3) 16) (+ 9 16) 25

6.001 SICP 10 Drill -- substitution (define x-y*y (lambda (x y) (- x ((lambda (x) (* x x)) y)))) (x-y*y 11 3) … (- 11 ((lambda (x) (* x x)) 3)) (- 11 (* 3 3)) (- 11 9) 2

6.001 SICP 11 Recursion Overview Recursion: Solve a problem by solving a smaller instance and converting into desired solution e.g., n! = n(n-1)(n-2) … = n * (n-1)! when does it end (base case)? 1! = 1 Recursive procedure: a nested call, a procedure that literally calls itself. e.g., (define (rec a) … (rec (+ a 1)) …) Recursive process: a process (internal sequence of execution) that calls itself with pending operations Key question of this lecture: how can an iterative processes be described by a recursive procedure?

6.001 SICP 12 General form of recursive algorithms test, base case, recursive case (define fact (lambda (n) (if (= n 1) ; test for base case 1 ; base case (* n (fact (- n 1)) ; recursive case ))) base case: smallest (non-decomposable) problem recursive case: larger (decomposable) problem

6.001 SICP 13 Fun Given that inc adds one to its argument and dec subtracts one from its argument (inc x) ==> x+1 (dec x) ==> x-1 what does fun do? (define fun (lambda (a b) (if (= a 0) b (inc (fun (dec a) b))))) Is it recursive? == “is it a recursive process?”

6.001 SICP 14 Fun (define fun (lambda (a b) (if (= a 0) b (inc (fun (dec a) b))))) (fun 2 1) (if (= 2 0) 1 (inc (fun (dec 2) 1)))) (if #f 1 (inc (fun (dec 2) 1))) (inc (fun (dec 2) 1)) (inc (fun 1 1)) (inc (if (= 1 0) 1 (inc (fun (dec 1) 1))))) (inc (if #f 1 (inc (fun (dec 1) 1)))) (inc (inc (fun (dec 1) 1))) (inc (inc (fun 0 1))) (inc (inc (if (= 0 0) 1 (inc (fun (dec 0) 1)))))) (inc (inc 1)) 3

6.001 SICP 15 Sum-ints recursive Problem: Recursively sum ints from a to b… base case? recursive case? test? (define sum-recursive (lambda (first last) (if (> first last) 0 (+ first (sum-recursive (+ first 1) last)))))

6.001 SICP 16 Iteration What are the elements of an iteration ? state start action update stop return Problem: Sum ints from a to b… State is s ;;State Starting with s <- 0 ;;Start s <- s + a ;;Update a <- a + 1 ;;Update repeat stop when a > b. ;;Stop The answer is the value of s.;;Return

6.001 SICP 17 Iteration What are the elements of an iteration ? state start action update stop return How do we do this in scheme (i.e., in a recursive procedure)?

6.001 SICP 18 Iteration What are the elements of an iteration ? state start action update stop return How do we do this in scheme (i.e., in a recursive procedure)? …use a “helper function”: (define (helper [STATE] ) (cond ( [DONE?] [RETURN] ) (else [ACTION] (helper [UPDATE] )))) (define (main)(helper [START] ))

6.001 SICP 19 Iteration To sum ints from a to b;;State State is s ;;State Starting with s <- 0 ;;Start s <- s + a ;;Update a <- a + 1 ;;Update repeat stop when a > b. ;;Stop The answer is the value of s.;;Return (define (sum-helper [STATE] ) (cond ( [DONE?] [RETURN] ) (else [ACTION] (sum-helper [UPDATE] )))) (define (sum-ints first last)(sum-helper [START] ))

6.001 SICP 20 Sum-ints iterative (define sum-ints (lambda (first last) (sum-helper 0 first last))) (define sum-helper (lambda (sum current last) (cond ((> current last) sum) (else (sum-helper (+ sum current) (+ current 1) last)))))

6.001 SICP 21 Consider the following procedures… (define (our-display x) (display x) x) (define (count1 x) (cond ((= x 0) 0) (else (our-display x) (count1 (- x 1))))) (define (count2 x) (cond ((= x 0) 0) (else (count2 (- x 1)) (our-display x)))) What happens for each of : (count1 4) (count2 4) are these recursive?

6.001 SICP 22 Count down (define (our-display x) (print x) x) (define (count1 x) (cond ((= x 0) 0) (else (our-display x) (count1 (- x 1))))) (count1 4) (print 4)“ 4 “ (count1 3) (print 3) “ 3 “ (count1 2) (print 2) “ 2 “ (count1 1) (print 1)“ 1 “ (count2 0)

6.001 SICP 23 Count up (define (our-display x) (print x) x) (define (count2 x) (cond ((= x 0) 0) (else (count2 (- x 1)) (our-display x)))) (count2 4) (begin (count2 3) (our-display 4)) (begin (begin (count2 2) (our-display 3)) (our-display 4)) (begin (begin (begin (count2 1) (our-display 2)) … (count2 1) … (count2 0) … (print 1)“1” (print 2)“2” (print 3)“3” (print 4)“4”

6.001 SICP 24 Iterative count-up Write a function count-up-2, but is an iterative process. (define (count-up-2 x) (define (iter [STATE] ) (cond ( [DONE?] #t) (else [DO SOMETHING] (iter [UPDATE] )))) (iter [START] ) )

6.001 SICP 25 Iterative count-up Write a function count-up-2, but is an iterative process. (define (count-up-2 x) (define (iter [STATE] ) ;; THE COUNTER (cond ( [DONE?] #t) ;; > x ? (else [DO SOMETHING] ;; Print! (iter [UPDATE] )))) ;; Increment! (iter [START] ) ;; x MOD by, ;; so we end up at x! )

6.001 SICP 26 Iterative count-up (define (our-display x) (display x) x) (define (count-up-iterative x) (define (iter val) (cond ((> val x) #t) (else (our-display val) (iter (+ val 1))))) (iter 1) )

6.001 SICP 27 Multiply We can build MUL(A,B) from other primitive ops… Recursive definition: test, base case, recursive case? … add B to mul (A-1, B)…; mul(1,B)=B Iterative algorithm: state start action update stop return? …S=0; for C=B to 1 by -1, do S=S+A; return S

6.001 SICP 28 Iterative or Recursive? (define (mul1 n m) (if (= n 0) 0 (+ m (mul1 (- n 1) m)))) (define (mul2 n m) (define (help count ans) (if (= count 0) ans (help (- count 1) (+ m ans)))) (help n 0)) (define (mul3 n m) (define (help count) (if (= count 0) 0 (+ m (help (- count 1))))) (help n)) Hint: which has pending operations?

6.001 SICP 29 Remember… calendar…