מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.

Slides:



Advertisements
Similar presentations
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
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.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Let* List and pairs manipulations –Insertion Sort Abstraction Barriers –Fractals –Mobile 2.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Data directed programming Section 2.4, pages ,2.5.2 pages (but with a different example)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Environment Model 3.2, pages
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
מבוא מורחב למדעי המחשב בשפת Scheme בוחן אמצע אביב 2006 פתרון לדוגמא.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Metacircular Evaluator 4.1, pages definitions file on web 2.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
Data Abstraction… The truth comes out…. What we’re doing today… Abstraction ADT: Dotted Pair ADT: List Box and Pointer List Recursion Deep List Recursion.
Extended Introduction to CS Preparation for Final Exam.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Data directed programming Message passing Section 2.4, pages ,2.5.2 pages (but with.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Outline Symbols Data Directed Programming, Message Passing.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
1 Lecture 16: Lists and vectors Binary search, Sorting.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
Functional Programming in Scheme and Lisp.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
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.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
1 מבוא מורחב למדעי המחשב בשפת Scheme תרגול Outline Mutable list structure RPN calculator Vectors and sorting.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
מבוא מורחב שיעור 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.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Additional Scheme examples
Edited by Original material by Eric Grimson
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
Chapter 15 – Functional Programming Languages
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
COP4020 Programming Languages
Env. Model Implementation
6.001 SICP Data abstractions
The Metacircular Evaluator
Lecture #8 מבוא מורחב.
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture #9 מבוא מורחב.
Lecture 13 - Assignment and the environments model Chapter 3
Data Mutation Primitive and compound data mutators set! for names
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
Lecture #7 מבוא מורחב.
List and list operations (continue).
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
To understand recursion, one must first understand recursion.
Presentation transcript:

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1

Outline More list examples Symbols 2

3 Triplets Constructor –(make-node value down next) Selectors –(value t) –(down t) –(next t)

4 skip

5 skip code (define (skip lst) (cond ((null? lst) lst) ((= (random 2) 1) (make-node ________________ ________________ ________________ )) (else (skip ________________ )))) (value lst) lst (skip (next lst)) (next lst)

6 skip1 (define (skip1 lst) (make-node (value lst) lst (skip (next lst)))) Average length: (n+1)/2 Running Time:  (n)

7 recursive-skip

8 recursive-skip1 code (define (recursive-skip1 lst) (cond ((null? (next lst)) __________ ) (else ___________________________ ))) lst (recursive-skip1 (skip1 lst))

9 make-star g*(x) = number of times we need to apply g until g(g(g(…(g(x)…)))<=1

10 make-star (define (make-star g) (define (g* x) (if (<= x 1) 0 (+ 1 (g* (g x))))) g* )

11 log (log 4) ==> 2 (log 1) ==> 0 (log 5) ==> 3 (define log (make-star ____________________ ) (lambda (x) (/ x 2))

12 compose-stars Input: a list of functions –(f g h) Output: a composite function: –fgh*(x) := f*(g*(h*(x))) Idea: –Make a list of star functions –Compose them together

13 compose-stars (define (compose-stars lst) (accumulate _______________________________ (map __________________________ __________________________ ))) compose (lambda (x) x) make-star lst

Example: Horner Rule 14 Evaluating a polynomial in x at a given value of x Algorithm :Horner's rule

Horner Rule: Cont’d 15 (define (horner-eval x coeff-sequence) (accumulate (lambda (this-coeff higher-terms) (+ this-coeff (* x higher-terms))) 0 coeff-sequence)) P(x)=1+2*x+2*x*x P(3)=25 > (horner-eval 3 '(1 2 2))

The Special Form quote 16

quote Number: does nothing '5=5 Name: creates a symbol 'a = (quote a) => a Parenthesis: creates a list and recursively quotes '(a b c) = (list 'a 'b 'c) = = (list (quote a) (quote b) (quote c)) => (a b c) 17

quote 'a => a (symbol? 'a) => #t (pair? 'a) => #f ''a => 'a (symbol? ''a) => #f (pair? ''a) => #t (car ''a) => quote (cdr ''a) => (a) ''''a => '''a (car ''''a) => quote (cdr ''''a) => (''a) 18

19 The predicate eq? A primitive procedure that tests if the pointers representing the objects point to the same place. Based on two important facts: A symbol with a given name exists only once. Each application of cons creates a new pair, different from any other previously created. Therefore: (eq? ‘a ‘a)  #t But, (eq? ‘(a b) ‘(a b))  #f

20 The predicate equal? A primitive procedure that tests if the pointers represent identical objects 1.For symbols, eq? and equal? are equivalent 2.If two pointers are eq?, they are surely equal? 3.Two pointers may be equal? but not eq? (equal? ‘(a b) ‘(a b))  #t (equal? ‘((a b) c) ‘((a b) c))  #t (equal? ‘((a d) c) ‘((a b) c))  #f

eq? vs. equal? (symbols) > (eq? ‘a ‘a) > (equal? ‘a ‘a) > (define x ‘a) > (define y ‘a) > (eq? x y) > (equal? x y) 21

(eq? (list 1 2 3) (list 1 2 3)) (equal? (list 1 2 3) (list 1 2 3)) (equal? (list (list (list 1) 2) (list 1)) (list (list (list 1) 2) (list 1))) (define x (list 1 2 3)) (define y (list 1 2 3)) (eq? x y) (define z y) (eq? z y) (eq? x z) eq? vs. equal? (symbols) 22

Example: Accumulate-n 23 Almost same as accumulate Takes third argument as “list of lists” Example: > (accumulate-n + 0 ‘((1 2 3) (4 5 6) (7 8 9) ( ))) ( )

Accumulate-n: Cont’d 24 (define (accumulate-n op init seqs) (if (null? (car seqs)) '() (cons (accumulate op init (map car seqs)) (accumulate-n op init (map cdr seqs)) )))

25 Example 1: memq (define (memq item lst) (cond ((null? lst) #f) ((eq? item (car lst)) lst) (else (memq item (cdr lst))))) (memq 'a '(b a b c)) => (memq 'a '((a b) b c)) => (memq 'a '((a b) b a (c a) d)) => (a b c) #f (a (c a) d) If item does not appear in lst, returns false. Otherwise – returns the sublist beginning with item.

26 Example 2: rember (define (rember item lst) (cond ((null? lst) '()) ((eq? item (car lst))(cdr lst)) (else (cons (car lst) (rember item (cdr lst))))) ) (rember 'a '(d a b c a)) => (rember 'b '(a b c)) => (rember 'a '(b c d)) => (d b c a) (a c) (b c d) Removes the first occurrence of item from lst.

27 Example 3: rember* (define (rember* item lst) (cond ((null? lst) '()) ((atom? (car lst)) (if (eq? (car lst) item) (rember* item (cdr lst)) (cons (car lst) (rember* item (cdr lst))))) (else (cons (rember* item (car lst)) (rember* item (cdr lst)))))) (rember* 'a '(a b)) => (rember* 'a '(a (b a) c (a)) => (b) ((b) c ()) Removes all occurrences of item from lst, at all levels

Quine 28 ((lambda (x) (list x (list 'quote x))) '(lambda (x) (list x (list 'quote x)))) A quine is a computer program that produces its own source code as its only output.

Split > (define syms '(p l a y - i n - e u r o p e - o r - i n - s p a i n)) > (split syms ‘-) ((p l a y) (i n) (e u r o p e) (o r) (i n) (s p a i n)) 29

Split (define (split symbols sep) (define (update sym word-lists) (if (eq? sym sep) (cons ___________________________________ ___________________________________ ) (cons ___________________________________ ___________________________________))) (accumulate update (list null) symbols)) null word-lists (cons sym (car word-lists)) (cdr word-lists) 30

Replace > (define syms '(p l a y - i n - e u r o p e - o r - i n - s p a i n)) > (replace ‘n ‘m syms) (p l a y – i m – e u r o p e – o r – i m – s p a i m) (define (replace from-sym to-sym symbols) (map )) (lambda (s) (if (eq? from-sym s) to-sym s)) symbols) 31

Accum-replace > (accum-replace ‘((a e) (n m) (p a)) syms) (p l a y – i n – e u r o p e – o r – i n – s p a i n) (a l a y – i n – e u r o a e – o r – i n – s a a i n) (a l a y – i m – e u r o a e – o r – i m – s a a i m) (e l e y – i m – e u r o e e – o r – i m – s e e i m) 32

Accum-replace (define (accum-replace from-to-list symbols) (accumulate (lambda(p syms) ( ________________________________ )) ____________________ from-to-list)) )) replace (car p) (cadr p) syms symbols 33

Extend-replace > (extend-replace ‘((a e) (n m) (p a)) syms) (p l a y – i n – e u r o p e – o r – i n – s p a i n) (a l a y – i n – e u r o a e – o r – i n – s a a i n) (a l a y – i m – e u r o a e – o r – i m – s a a i m) (a l e y – i m – e u r o a e – o r – i m – s a e i m) 34

Extend-replace (define (extend-replace from-to-list symbols) (define (scan sym) (let ((from-to (filter _____________________________________ _____________________________________ ))) (if (null? from-to) ___________________________ ___________________________))) (map scan symbols)) (lambda (p) (eq? (car p) sym)) from-to-list sym (cadr (car from-to)) 35