CS220 Programming Principles

Slides:



Advertisements
Similar presentations
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Advertisements

מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
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 – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
1 Gentle Introduction to Programming Session 4: Arrays, Sorting, Efficiency.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
1 Gentle Introduction to Programming Session 2: Functions.
Scheme Tutorial. Goals Combine several simple ideas into one compound idea to obtain complex ideas Bring two ideas together to obtain relations Seperate.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 13: Streams 한 태숙.
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,
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
PPL Lecture 3 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
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.
9-October-2002cse MoreLambda © 2002 University of Washington1 More Lambda CSE 413, Autumn 2002 Programming Languages
160 as a product of its prime factors is 2 5 x 5 Use this information to show that 160 has 12 factors.
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
Higher-Order Programming: Iterative computation (CTM Section 3
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Lecture #5 מבוא מורחב.
CS314 – Section 5 Recitation 10
Functional Programming
CS 550 Programming Languages Jeremy Johnson
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
PPL Lecture 3 Slides by Yaron Gonen,
Computing Square Roots
Higher-Order Programming: Iterative computation (CTM Section 3
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Material in the textbook on pages
Haskell.
CS21b: Structure and Interpretation
6.001 SICP Data abstractions
Higher-Order Procedures
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.
Lecture #5 מבוא מורחב.
Lecture 18 Infinite Streams and
This Lecture Substitution model
Lecture #6 section pages pages72-77
Lecture #6 מבוא מורחב.
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.
Square Root.
6.001 SICP Streams – the lazy way
Extended Introduction to Computer Science
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.
Material in the textbook Sections to 1.2.1
Lecture #6 section pages pages72-77
6.001 SICP Variations on a Scheme
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
3.8 Newton’s Method How do you find a root of the following function without a graphing calculator? This is what Newton did.
Today’s topics Abstractions Procedural Data
Lecture 2 מבוא מורחב.
This Lecture Substitution model
CSE S. Tanimoto Lambda Calculus
Introduction to the Lab
This Lecture Substitution model
topics interpreters meta-linguistic abstraction eval and apply
Lecture 2 מבוא מורחב.
Rules of evaluation The value of a number is itself.
Presentation transcript:

CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 3 한 태숙

Higher Order Procedure Building Abstraction : assign a name to common pattern and then work in terms of the abstraction directly Accept procedure as arguments or return procedures as values (define (cube x) (* x x x)) (define (hf f x) (f x))

A simple common pattern (* 2 2) (* 3 3) (* 4 4) is captured by (define square (lambda (x) (* x x)) (define (square x) (* x x)) Provides a NAME to the idea of multiplying something by itself

Using lambda for Local Name Want to compute f(x) = x2/1-x2 We don’t want to compute x2 twice (define (f x) ((lambda (xsq) (/ xsq (- 1 xsq))) (square x)))

Syntactic sugar (define (inc x) (+ 1 x)) (define inc (lambda (x) (+ 1 x))) (define (f x) (let ((xsq (square x))) (/ xsq (- 1 xsq))))

Three Sums 1 + 2 + … + 100 12+ 22+ … + 1002 1/12 + 1/32 + … + 1/992 (define (sum-integers a b) (if (> a b) (+ a (sum-integers (+ a 1) b))))

(define (sum-squares a b) (if (> a b) (+ (square a) (sum-squares (+ a 1) b)))) (define (pi-sum a b) (+ (/ 1 (square a)) (pi-sum (+ a 2) b)))) Sch-Num x Sch-Num -> Sch-Num

Type and Pattern of Procedure (define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) F x Sch-Num x F x Sch-Num -> Sch-Num where F =(Sch-Num -> Sch-Num)

Sum as a high-order function (define (inc i) (+ i 1)) (define (sum-integers a b) (sum (lambda (x) x) a inc b)) (define (sum-squares a b) (sum square a inc b)) (define (pi-sum a b) (sum (lambda (i)(/ 1 (square i))) a (lambda (i) (+ i 2)) b))

Numerical Integration òabf dx =[f(a)+f(a+dx)+…+f(b)]dx (define (integral f a b dx) (define (add-dx x) (+ x dx) (* (sum f a add-dx b) dx)) (integral square 0 1 0.001) (integral (lambda (x) (* x x x)) 0 1 0.001)

Exercise 1.30 (define (sum term a next b) (define (iter i result) (if < > < > (iter < > < >))) (iter < > < >)) ( > i b) result (next i) (+ (term i) result) a

Constructing with Lambda (lambda (<formal-param>) <body>) (define (plus4 x) (+ x 4)) (define plus4 (lambda (x) (+ x 4))) ((lambda (x y z)(+ x y (square z))) 1 2 3) (define (integral f a b dx) (* (sum f (+ a dx) (lambda (x)(+ x dx)) b) dx))

Defining Local Variables(I) (define (f x y) (define a (+ 1 (* x y))) (define b (- 1 y)) (+ (* x (square a)) (* y b) (* a b)))

Defining Local Variables(II) (define (f x y) (define (f-helper a b) (+ (* x (square a)) (* y b) (* a b))) (f-helper (+ 1 (* x y)) (- 1 y)))

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

Defining Local Variables(IV) (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b))))

Let expression (let ((<var1> <exp1>) … (<varn> <expn>)) <body>) is syntactic sugar for ((lambda (<var1> … <varn>) <body>) <exp1> … <expn>)

Let Expression - Semantics Scope of a variable is the body of the let (define x 5) (+ (let ((x 3))(+ x (* x 10))) x) ; --> 38 Variables are bound simultaneously, using values computed outside the let (define x 2) (let ((x 3) (y (+ x 2))) (* x y)) ; -> 12

Exercise 1.34 (define (f g) (g 2)) > (f square) 4 > (f (lambda (z)(* z (+ z 1)))) 6 > (f f) ???

Compute Square Roots - Revisit Computing Square root of x is a fixed point of the function f(y) = x/y ( = y ) Fixed point of function f mean find y such that f(y) = y start with a guess for y keep applying f over and over until the result doesn’t change very much

Fix-point Operator Fixed-point (define tolerance 0.001) (define (close? u v) (< (abs (- u v)) tolerance)) (define (fixed-point f i-guess) (define (try guess) (let ((next-guess (f guess))) (if (close? next-guess guess) next-guess (try next-guess)))) (try i-guess))

Square root of X as a fixed point Fixed Point of the function f(y) = x/y (define (sqrt x) (fixed-point (lambda (y) (/ x y)) 1)) It does not work, because it doesn’t converge - it oscillates

Average Damping (define (average-damp f) (lambda (x) (average x (f x)))) (define (average-damp (lambda (f) (lambda (x) (average x (f x))))) (Sch-Num -> Sch-Num) -> (Sch-Num -> Sch-Num)

Safe Square Root as a fixed point (define (sqrt x) (fixed-point (average-damp (lambda (y) (/ x y))) 1)) Linear Iterative Process?

Conventional Approach (define (sqrt x) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (close? (square guess) x) guess (sqrt-iter (improve guess)))) (sqrt-iter 1))

Why use Fixed-point? Using fixed-point approach makes us see the idea more clearly Find cube root -> Fixed-point of y -> x/y2 (define (cuberoot x) (fixed-point (average-damp (lambda (y)(/ x (square y)))) 1))

Procedures as Returned Values Derivative of function f : Df Df(x) = [f(x+dx) - f(x)] / dx (Sch-Num -> Sch-Num) -> (Sch-Num -> Sch-Num) (define (deriv f) (lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))

Derivative of Fun : Df (define deriv (lambda (f) (let ((dx 0.00001)) (lambda (x) (/ (- (f (+ x dx)) (f x)) dx))))) ((deriv square) 10) ----> 20.0000…….

Newton’s Method To find a zero of a function g such that g(y)=0 Find a fixed-point of the function f(y) f(y) = y - g(y)/Dg(y)

Newton’s Method: program (define (newton-transform g) (lambda (y) (- y (/ (g y)((deriv g) y))))) (define (newtons-method g guess) (fixed-point (newton-transform g) guess)) Example: Square root of x is zero of g(y)=y2-x

Example :Square Root (define (sqrt x) (newtons-method (lambda (y) (- (square y) x)) 1.0)) (fixed-point (average-damp (lambda (y) (/ x y))) 1))

Abstraction and First-class Proc More general idea for square root as fixed points (define (fixed-point-of-transform g transform guess) (fixed-point (transform g) guess))

(define (sqrt x) (fixed-point-of-transform (lambda (y) (- (square y) x)) newton-transform 1)) (lambda (y) (/ x y)) average-damp

First Class Citizens May be named by variables May be passed as arguments to procedures May be returned as the results of procedures May be included in data structures