Functional Programming

Slides:



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

CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Structure and Interpretation of Computer Programs Presented by Yan Yan CSE 294, UCSD, April 13, Chapter Harold Abelson, Gerald and Julie.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
מבוא מורחב 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.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
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.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Spring 2008Programming Development Techniques 1 Topic 5.5 Higher Order Procedures (This goes back and picks up section 1.3 and then sections in Chapter.
From Lambda Calculus to LISP Functional Programming Academic Year Alessandro Cimatti
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
ARITHMETIC IN C Operators.
Lesson #6 Modular Programming and Functions.
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Lesson #6 Modular Programming and Functions.
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.
Closures and Streams cs784(Prasad) L11Clos
Original material by Eric Grimson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
CS21b: Structure and Interpretation
Lesson #6 Modular Programming and Functions.
Applied Algorithms (Lecture 17) Recursion Fall-23
Algorithm design and Analysis
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
Lecture 18 Infinite Streams and
FP Foundations, Scheme In Text: Chapter 14.
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 Further Variations on a Scheme
Extended Introduction to Computer Science
Streams, Delayed Evaluation and a Normal Order Interpreter
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.
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
Lesson #6 Modular Programming and Functions.
Lecture 2 מבוא מורחב.
Application: Algorithms
Application: Algorithms
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
CSE S. Tanimoto Lambda Calculus
Introduction to the Lab
This Lecture Substitution model
Lecture 2 מבוא מורחב.
Presentation transcript:

Functional Programming Part 2

Outline Previous lecture Today’s lecture Functional Programming Lecture LISP Some basics of Scheme Today’s lecture More on Scheme determine the meaning of scheme expressions by developing a precise model for interpreting them model: Substitution Model Linear recursive, Linear iterative, Tree recursive processes

Scheme Expressions Primitive expression Compound expression Example: Numbers Compound expression Combination of primitive expressions (+ 123 456) When you type an expression, the Scheme interpreter responds by displaying the result of its evaluating that expression

Scheme combinations Expressions formed by delimiting a list of expressions within parentheses in order to denote procedure application (fun op1 op2 op3 …) delimited by parentheses first element is the Operator the rest are Operands

Some simple combinations (+ 2 3) (- 4 1) (+ 2 3 4) (abs -7) (abs (+ (- x1 x2) (- y1 y2)))

Meaning? What does a scheme expression mean? How do we know what value will be calculated by an expression? (abs (+ (- x1 x2) (- y1 y2)))

Substitution Model To evaluate a scheme expression: Evaluate each of the operands Evaluate the operator Apply the operator to the evaluated operands 2 1 (fun op1 op2 op3 …) 3

Example of expression evaluation (+ 3 (* 4 5) ) operator operand 1 operand 2

Example of expression evaluation evaluate 3  3 evaluate (* 4 5) evaluate 4  4 evaluate 5  5 evaluate *  * (multiplication) apply * to 4, 5  20 evaluate +  + (addition) apply + to 3, 20  23

Primitive expressions Numbers evaluate to themselves 105  105 Primitive procedures evaluate to their corresponding internal procedures +, -, *, /, abs … e.g. “+” evaluates to the procedure that adds numbers together

Another example (+ 3 (- 4 (* 5 (expt 2 2)))) evaluate 3  3 evaluate (- 4 (* 5 (expt 2 2))) evaluate 4  4 evaluate (* 5 (expt 2 2)) evaluate 5  5 evaluate (expt 2 2) evaluate 2  2 apply expt to 2, 2  4 apply * to 5, 4  20 apply – to 4, 20  -16 apply + to 3, -16  -13 Note: Operator evaluation step was skipped

Evaluation with variables Association between a variable and its value (define X 3) To evaluate a variable: look up the value associated with the variable e.g., X has the value of 3 and replace the variable with its value

Simple expression evaluation (define X 3) (define Y 4) (+ X Y) evaluate X  3 evaluate Y  4 evaluate +  [primitive procedure +] apply + to 3, 4  7

Special forms There are a few special forms which do not evaluate in quite the same way as we’ve described define is one of them (define X 3) We do not evaluate X before applying define to X and 3 Instead, we simply evaluate the second argument (3) Associate the second argument (3) to the first argument (X)

Lambda Expressions lambda is also a special form: Result of a lambda is always a function We do not evaluate its contents just “save them for later”

Scheme function definition A(r)= pi * r2 (define A (lambda (r) (* pi (expt r 2)))) body of function argument(s) a function

Evaluate lambda (define A (lambda (r) (* pi (expt r 2)))) evaluate (lambda (r) (* pi (expt r 2))) create procedure with one argument r and body (* pi (expt r 2)) create association between A and the new procedure

Evaluate a function call To evaluate a function call: [use the standard rule] evaluate the arguments apply the function to the (evaluated) arguments To apply a function call: Substitute each argument with its corresponding value everywhere in the function body Evaluate the resulting expression

Example 1 (define f (lambda (x) (+ x 1))) Evaluate (f 2) evaluate f  (lambda (x) (+ x 1)) apply (lambda (x) (+ x 1)) to 2 Substitute 2 for x in the expression (+ x 1)  (+ 2 1) Evaluate (+ 2 1)  3

Example 2 (define f (lambda (x y) (+ (* 3 x) (* -4 y) 2)))) Evaluate (f 3 2) evaluate 3  3 evaluate 2  2 evaluate f  (lambda (x y) (+ (* 3 x) (* -4 y) 2)) apply (lambda (x y) …) to 3, 2 Substitute 3 for x, 2 for y Evaluate (+ (* 3 3) (* -4 2) 2))  3

Equivalent forms of expressions Equivalent expressions: (define f (lambda (x) (+ x 1)) (define (f x) (+ x 1)) To evaluate: (define (f x) (+ x 1)) convert it into lambda form: (define f (lambda (x) (+ x 1))) evaluate like any define: create the function (lambda (x) (+ x 1)) create an association between the name f and the function

Example (define sq (lambda (x) (* x x)) (define d (lambda (x y) (+ (sq x) (sq y)))) evaluate: (d 3 4) evaluate 3  3 evaluate 4  4 evaluate d  (lambda (x y) (+ (sq x) (sq y))) apply (lambda (x y) …) to 3, 4

Example (cont’d) Apply (lambda (x y) (+ (sq x) (sq y))) to 3, 4 substitute 3 for x, 4 for y in (+ (sq x) (sq y)) evaluate (+ (sq 3) (sq 4)) evaluate (sq 3) evaluate 3  3 evaluate sq  (lambda (x) (* x x)) apply (lambda (x) (* x x)) to 3 substitute 3 for x in (* x x) evaluate (* 3 3) apply * to 3, 3  9

Example (cont’d) Apply (lambda (x y) (+ (sq x) (sq y))) to 3, 4 substitute 3 for x, 4 for y in (+ (sq x) (sq y)) evaluate (+ (sq 3) (sq 4)) evaluate (sq 3)  [many steps, previous slide]  9 evaluate (sq 4) evaluate 4  4 evaluate sq  (lambda (x) (* x x)) apply (lambda (x) (* x x)) to 4 substitute 4 for x in (* x x) evaluate (* 4 4) apply * to 4, 4  16

Example (cont’d) Apply (lambda (x y) (+ (sq x) (sq y))) to 3, 4 substitute 3 for x, 4 for y in (+ (sq x) (sq y)) evaluate (+ (sq 3) (sq 4)) evaluate (sq 3)  [many steps, 2 slides back]  9 evaluate (sq 4)  [many steps, previous slide]  16 evaluate +  [primitive procedure +] apply + to 9 and 16  25 which is final result Therefore, (d 3 4)  25

Substitution Model Gives precise model for evaluation Can carry out mechanically by you by the computer The model is simple but tedious to evaluate all those steps!

Lambdas evaluating to booleans Already seen functions that evaluate to numbers: (define (circle-area r) (* pi (expt r 2))) (circle-area 1) evaluates to 3.1415… Now, functions that evaluate to booleans (define (at-least-two-true a b c) (or (and a b) (and b c) (and a c)))

How does If expression evaluate? Evaluate the <boolean-expression> If true, evaluate the <true-case-expression> Otherwise, evaluate the <false-case-expression> The whole if-expression then evaluates to the result of either <true-case-expr> or <false-case-expr> (if <boolean-expression> <true-case-expression> <false-case-expression>)

Formal substitution with if (define (max a b) (if (> a b) a b)) Evaluate (max 3 2) Evaluate 3  3 Evaluate 2  2 Evaluate max  (lambda (a b) (if (> a b) a b)) Apply (lambda (a b) (if (> a b) a b)) to 3,2 Substitute 3 for a, 2 for b in (if (> a b) a b) Evaluate (if (> 3 2) 3 2) Evaluate (> 3 2) … #t (if #t 3 2) Evaluate 33

If is an Expression Since “if” is an expression, it “returns a value” or “evaluates to a value” This is different from many programming languages You can use that value

Example (define (scale a b) (/ (if (> a b) a b) (if (> a b) b a))) Evaluate: (scale 4 2) Evaluate 4  4 Evaluate 2  2 Evaluate scale  (/ (if (> a b) a b) (if (> a b) b a))) Apply (lambda (a b) (/ (if (> a b) a b) (if (> a b) b a))) to 4,2 Substitute 4 for a, 2 for b (/ (if (> 4 2) 4 2) (if (> 4 2) 2 4)) (/ (if #t 4 2) (if #t 2 4)) (/ 4 2) 2

Recursion Example: How do I compute the sum of the first N integers? Sum of first N integers = N + N-1 + N-2 + … 1 N + ( N-1 + N-2 + … 1 ) N + [Sum of first N-1 integers] Convert to scheme: (define (sum-integers n) (if (= n 0) (+ n (sum-integers (- n 1)))))

Evaluating Evaluate: (sum-integers 3) (if (= 3 0) 0 (+ 3 (sum-integers (- 3 1)))) (if #f 0 (+ 3 (sum-integers (- 3 1)))) (+ 3 (sum-integers (- 3 1))) (+ 3 (sum-integers 2)) (+ 3 (if (= 2 0) 0 (+ 2 (sum-integers (- 2 1))))) (+ 3 (if #f 0 (+ 2 (sum-integers (- 2 1))))) (+ 3 (+ 2 (sum-integers -2 1))) (+ 3 (+ 2 (sum-integers 1))) (+ 3 (+ 2 (if (= 1 0) 0 (+ 1 (sum-integer (- 1 1)))))) (+ 3 (+ 2 (if #f 0 (+ 1 (sum-integer (- 1 1)))))) (+ 3 (+ 2 (+ 1 (sum-integer (- 1 1)))))) (+ 3 (+ 2 (+ 1 (sum-integer 0))))) (+ 3 (+ 2 (+ 1 (if (= 0 0) 0 (+ 0 (sum-integer (- 0 1))))))) (+ 3 (+ 2 (+ 1 (if #t 0 (+ 0 (sum-integer (- 0 1))))))) (+ 3 (+ 2 (+ 1 0))) … 6

Linear Recursive This is what we call a linear recursive process Makes linear calls Keeps a chain of deferred operations linear w.r.t. input (sum-integers 3) (+ 3 (sum-integers 2)) (+ 3 (+ 2 (sum-integers 1))) (+ 3 (+ 2 (+ 1 (sum-integers 0)))) (+ 3 (+ 2 (+ 1 0)))

Another strategy To sum integers… Add up as we go along: Current Sum 1 2 3 4 5 6 7 …… 1 3 6 10 15 21 28 …… Current Sum 0 0 1 1 + 0 = 1 2 2 + 1 = 3 3 3 + 3 = 6 4 4 + 6 = 10 5 5 + 10 = 15

Alternate definition (define (sum-int n) (sum-iter 0 n 0)) (define (sum-iter current max sum) (if (> current max) sum (sum-iter (+ 1 current) max (+ current sum))))

Evaluation of sum-int (sum-int 3) (sum-iter 0 3 0) (if (> 0 3) …. (define (sum-int n) (sum-iter 0 n 0)) (define (sum-iter current max sum) (if (> current max) sum (sum-iter (+ 1 current) (+ current sum)))) (sum-int 3) (sum-iter 0 3 0) (if (> 0 3) …. (sum-iter 1 3 0) (if (> 1 3) …. (sum-iter 2 3 1) (if (> 2 3) …. (sum-iter 3 3 3) (if (> 3 3) … (sum-iter 4 3 6) (if (> 4 3) …) 6

Difference from Linear Recursive (sum-integers 3) (+ 3 (sum-integers 2)) (+ 3 (+ 2 (sum-integers 1))) (+ 3 (+ 2 (+ 1 (sum-integers 0)))) (+ 3 (+ 2 (+ 1 0))) (Linear Recursive Process) (sum-int 3) (sum-iter 0 3 0) (sum-iter 1 3 0) (sum-iter 2 3 1) (sum-iter 3 3 3) (sum-iter 4 3 6)

Linear Iterative (sum-int 3) (sum-iter 0 3 0) (sum-iter 1 3 0) This is what we call a linear iterative process Makes linear calls State of computation kept in a constant number of state variables Does not grow with problem size (sum-int 3) (sum-iter 0 3 0) (sum-iter 1 3 0) (sum-iter 2 3 1) (sum-iter 3 3 3) (sum-iter 4 3 6) 6

Fibonacci Numbers Fibonacci sequence 0, 1, 1, 2, 3, 5, 8, 13, 21 What’s the rule for this sequence? Fib(0) = 0 Fib(1) = 1 Fib(N) = Fib(N-1) + Fib(N-2)

Simple Scheme Translation What’s the rule for this sequence? Fib(0) = 0 Fib(1) = 1 Fib(N) = Fib(N-1) + Fib(N-2) (define (fib n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) Has two recursive calls for each evaluation

Evolution of fib (fib 5) (+ (fib 4) (fib 3)) (+ (+ (+ (fib 2) (fib 1)) (+ (fib 1) (fib 0))) (+ (+ (fib 1) (fib 0)) 1)) (+ (+ (+ (+ (fib 1) (fib 0)) 1) (+ 1 0))) (+ (+ 1 0) 1)) (+ (+ (+ (+ 1 0) 1) (+ 1 0)) (+ (+ 1 0) 1)

(fib n) (fib (- n 1)) (fib (- n 2)) Fib Evolution (fib (- n 2)) Expands into a “tree” instead of linear chain (fib n) (fib (- n 1)) (fib (- n 2)) (fib (- n 2)) (fib (- n 3)) (fib (- n 3)) (fib (- n 4))

Tree Recursion (fib 5) (+ (fib 4) (fib 3)) (+ (+ (fib 3) (fib 2)) This is what we call a tree recursive process Calls fan out in a tree How many calls to fib? Exponential in call argument (Not perfectly “balanced” tree)

Alternate Method Again, count up n: 0 1 2 3 4 5 6 7 … Fib(n): 0 1 1 2 3 5 8 13 …. Example: Fib(5) Count Fnext Fib 5 1 0 4 1 1 3 1+1=2 1 2 2+1=3 2 1 3+2=5 3 0 5+3=8 8

Linear Iterative (define (ifib n) (define (fib-iter fnext f cnt) (fib-iter 1 0 n)) (define (fib-iter fnext f cnt) (if (= cnt 0) f (fib-iter (+ fnext f) fnext (- cnt 1))))

Evolution of Iterative Fib (define (fib-iter fnext f cnt) (if (= cnt 0) f (fib-iter (+ fnext f) fnext (- cnt 1)))) (ifib 5) (fib-iter 1 0 5) (fib-iter 1 1 4) (fib-iter 2 1 3) (fib-iter 3 2 2) (fib-iter 5 3 1) (fib-iter 8 5 0) 5