CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.

Slides:



Advertisements
Similar presentations
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
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.
6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
Fall 2008Programming Development Techniques 1 Topic 10 Example: Symbolic Differentiation Section October 2008.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
SICP Symbolic data Symbol: a primitive type Generalization Symbolic differentiation.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
6.001 SICP 1/ : Structure and Interpretation of Computer Programs Symbols Example of using symbols Differentiation.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
D x (c) = 0 D x (x) = 1 D x (y) = 0 for y an independent variable D x (u+v) = D x (u) + D x (v) D x (uv) = u D x (v) + v D x (u) D x (u n ) = nu n-1 D.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Introduction to Scheme. Lisp and Scheme Lisp: List processor language –Full recursion –Conditional expression –Extensibility –Interactive Scheme: one.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
SICP Tagged data Why do we need tags Concept of tags Extended example.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
SICP Tagged data Why do we need tags Concept of tags Extended example.
Functional Programming
Abstract Syntax cs7100 (Prasad) L7AST.
CS314 – Section 5 Recitation 10
Introduction to Scheme
6.001: Structure and Interpretation of Computer Programs
Chapter 15 – Functional Programming Languages
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lists in Lisp and Scheme
Original material by Eric Grimson
LISP A brief overview.
The Metacircular Evaluator
Abstract Syntax Prabhaker Mateti 1.
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
The Metacircular Evaluator
CS 36 – Chapter 11 Functional programming Features Practice
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Chapter 1 Review: BNF Grammar for lists:
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture #9 מבוא מורחב.
Abstract Syntax cs7100 (Prasad) L7AST.
LISP A brief overview.
Data Mutation Primitive and compound data mutators set! for names
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
topics mutable data structures
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture #7 מבוא מורחב.
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
Modern Programming Languages Lecture 18 Fakhar Lodhi
Lists in Lisp and Scheme
Lecture 25: The Metacircular Evaluator Eval Apply
Dispatch on Type (one-less x) => x-1 if x is a <number>
Presentation transcript:

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

2 Symbolic Data n A Symbol is a new primitive data type n Value vs. Symbol (a b c d) ( ) ((Norah 12)(Molly 9)(Anna 7)) (* ( ) (+ x 9)) (define (fact n) (if (= n 1) 1 (* n (fact (- n 1)))))

3 Quote n We create symbol with “quote” (define x 23) x ; ==> 23 (quote x) ; ==> x ’x (quote (a b c)) ’(a b c) (car ’(a b c)) (car (quote (a b c)))

4 Symbol n Evaluate to? Print as? (define z ’y) z (+ x 3) (list + x 3) (list ’+ ’x ’3) ’(list + x 3) ’(2 a) ’(2 (b 3)) (car ’(a b c))

5 EQ? EQV? EQUAL? ( eqv? obj1 obj2) returns #t if obj1 and obj2 are –both #t or both #f –both symbol and (string=? (symbol->string obj1) (symbol->string obj2)) => #t –both numbers, numerically equal, and either both exact or both inexact –both characters and the same character according to char=? procedure

6 EQV? ( eqv? obj1 obj2) returns #t if obj1 and obj2 are –the empty list –pairs, vectors or strings that denote the same locations in the store –procedures whose location tags are equal

7 Eqv? Returns #f ( eqv? obj1 obj2) returns #f if: –obj1 and obj2 are of different types –one of obj1 and obj2 is #t but the other is #f –obj1 and obj2 are symbols but (string=? (symbol->string obj1) (symbol->string obj2) =>#f –one of obj1 and obj2 is an exact number but the other is an inexact number

8 Eqv? Returns #f (continue’d) ( eqv? obj1 obj2) returns #f if: –obj1 and obj2 are numbers(characters) for which the = (char=?) procedure returns #f –one of obj1 and obj2 is the empty list but the other is not –obj1 and obj2 are pairs, vectors, or strings that denote distinct locations –obj1 and obj2 are procedures that would behave differently for some arguments

9 Examples of Eqv? (eqv? ’a ’a) ==> #t (eqv? ’a ’b) ==> #f (eqv? 2 2) ==> #t (eqv? ’() ’()) ==> #t (eqv? ) ==> #t (eqv? (cons 1 2) (cons 1 2)) ==> #f (eqv? (lambda() 1) (lambda () 2)) ==> #f (eqv? #f ’nil) ==> #f (let ((p (lambda (x) x))) (eqv? p p)) ==> #t

10 (eqv? ’(a) ’(a)) ==> unspecified (eqv? ”a” ”a”) ==> unspecified (eqv? ’(b) (cdr ’(a b))) ==> unspecified (let ((x ’(a))) eqv? x x )) ==> #t

11 EQ? - similar to but finer than eqv? n guaranteed to have the same behavior on symbols, booleans, the empty list, pairs, procedures, and non-empty strings and vectors n may be implementation-dependent on numbers and characters and will return true only when eqv? would also return true n may behave differently from eqv? on empty vectors and empty strings

12 Examples on Eq? (eq? ’a ’a) ==> #t (eq? ’(a) ’(a)) ==> unspecified (eq? (list ’a) (list ’a)) ==> #f (eq? ”a” ”a”) ==> unspecified (eq? ”” ””) ==> unspecified (eq? ’() ’()) ==> #t (eq? 2 2) ==> unspecified (eq? #\A #\A) ==> unspecified

13 (eq? car car) ==> #t (let ((n (+ 2 3))) (eq? n n)) ==> unspecified (let ((x ’(a))) (eq? x x)) ==> #t (let ((x ’#())) (eq? x x)) ==> #t (let ((p (lambda (x) x))) (eq? p p)) ==> #t

14 EQUAL? n Library procedure n recursively compare the contents of pairs, vectors, and strings, applying eqv? on other objects such as symbols and numbers. n Objects are generally equal? if the print the same.

15 Examples on Equal? (equal? ’a ’a) ==> #t (equal? ’(a) ’(a)) ==> #t (equal? ’(a (b) c) ’(a (b) c)) ==> #t (equal? ”abc” ”abc”) ==> #t (equal? 2 2) ==> #t (equal? (make-vector 5 ’a) (make-vector 5 ’a)) ==> #t (equal? (lambda (x) x) (lambda (y) y)) ==> unspecified

16 Memq - with eq? n (symbol, list ) -> #f if none is the same in list sublist with symbol otherwise (define (memq item x) (cond ((null? x) #f) ((eq? item (car x)) x) (else (memq item (cdr x)))))

17 Exercise 2.53 (list ’a ’b ’c) (list (list ’george)) (cdr ’((x1 x2) (y1 y2))) (cadr ’((x1 x2) (y1 y2))) (pair? (cadr ’(a short list))) (memq ’red ’((red shoes) (blue socks))) (memq ’red ’(red shoes blue socks)) (car ’ ’abracadabra)

18 Exercise 2.54 n Implement equal? (equal? ’(this is a list) ’(this is a list)) ==> #t (equal? ’(this is a list) ’(this (is a) list)) ==> #f Plan: both symbols and eq? symbols both lists and equal? (car a) (car b) and equal? (cdr a) (cdr b)

19 Differentiation n Numerical Computation (define (numerical-derivative f) (define epsilon ) (lambda (x) (/ (- (f (+ x epsilon)) (f x)) epsilon)))

20 Symbolic Differentiation (define (deriv exp var) (cond ((constant? exp)(make-constant 0)) ((variable? exp) (if (same-variable? exp var) (make-constant 1) (make-constant 0))) ((sum? exp) (make-sum (deriv (addend exp) var) (deriv (augend exp) var)))

21 ((product? exp) (make-sum (make-product (multiplier exp) (deriv (multiplicand exp) var)) (make-product (multiplicand exp) (deriv (multiplier exp) var)))) (else (error ”unknown expression type” exp))))

22 Math Expression Abstraction(I) (make-constant ) Construct constant (constant? ) Is a constant? (make-variable ) Construct a variable (variable? ) Is a variable? (same-variable ) Are and same?

23 Math Expression Abstraction(II) (make-sum ) Construct sum (sum? ) Is a sum? (addend ) Addend of sum (augend ) Augend of sum (make-product ) (product? ) Is a product? (multiplier ) multiplier of product (multiplicand ) multiplicand of product

24 Math Expression Implementation(I) Represent (ax+b) with prefix notation such as (+ (* a x) b) (define (make-constant x) x) (define (constant? x) (number? x))

25 Math Expression Implementation(II) (define (make-variable x) x) (define (variable? x) (symbol? x)) (define (same-variable? v1 v2) (and (variable? v1) (variable? v2) (eq? v1 v2)))

26 Math Expression Implementation(III) (define (make-sum a1 a2) (list ’+ a1 a2)) (define (sum? x) (and (pair? x) (eq? (car x) ’+))) (define (addend s) (cadr s) (define (augend s) (caddr s) (define (make-product m1 m2) (list ’* m1 m2)) (define (product? x) (and (pair? x) (eq? (car x) ’*))) (define (multiplier m) (cadr m) (define (multiplicand m) (caddr m)

27 Reducing Math Exp Implementation (define (make-sum a1 a2) (cond ((and (constant? a1) (constant? a2)) (make-constant (+ a1 a2))) ((constant? a1) (if (= a1 0) a2 (list ’+ a1 a2))) ((constant? a2) (if (= a2 0) a1 (list ’+ a1 a2))) (else (list ’+ a1 a2))))

28 (define (make-product m1 m2) (cond ((and (constant? m1) (constant? m2)) (make-constant (* m1 m2))) ((constant? m1) (cond ((= m1 0) (make-constant 0)) ((= m1 1) m2) (else (list ’* m1 m2)))) ((constant? m2) (cond ((= m2 0) (make-constant 0)) ((= m2 1) m1) (else (list ’* m1 m2))))

29 Adding Exponential Expression (define (deriv exp var) (cond ………. ((exponential? exp) (make-product (make-product (exponent exp) (make-exponential (base exp) (- (exponent exp) 1))) (deriv (base exp) var)))))

30 (define (make-exponential b e) (cond ((= e 0) (make-constant 1)) ((= e 1) b) (else (list ’** b e)))) (define exponential? exp) (and (pair? exp) (eq? (car exp) ’**))) (define (base exp) (cadr exp)) (define (exponent exp) (caddr exp))

31 Dotted Tail Notation n Exercise 2.20 (define (f x. y) ) (f ) in x bound to 1 y bound to (2 3 4) (define (same-parity x. y) (same-parity ) x==> 1 y==>( )

32 Implementation(II) of Math Exp n Using Variable # Terms -> (+ a b c) (define (make-sum a1. a2) (cons ’+ (cons a1 a2))) (define (augend s) (if (null? (cdddr s)) (caddr s) (cons ’+ (cddr s)))) (define (multiplicand p) (if (null? (cdddr p)) (caddr p) (cons ’* (cddr p))))