מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list 0 1 0 2)) ==> (0 0 0 1) (scramble.

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

Programming with Lists
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully.
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב - שיעור 15 1 Lecture 15 Streams. מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((>
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
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.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 13. Streams 3.5, pages definitions file on web 2.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב - שיעור 12 1 Lecture 12 Data directed programming Message passing dotted-tail notation & apply Section 2.4, pages ,2.5.2 pages.
Quiz: Box and Pointer fun! (cons (cons (cons ‘hey (cons ‘there nil)) nil) (cons ‘wow nil)) (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me)))
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 13: Streams 한 태숙.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
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.
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
Today’s topic: Abstraction Compound Data Data Abstractions: Isolate use of data abstraction from details of implementation Relationship between data abstraction.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
PPL Lazy Lists. Midterm 2012 (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.
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.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Example of formula (defun roots (a b c) (list
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
(defmacro (delay x) (list 'lambda () x))
Racket CSC270 Pepper major portions credited to
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Closures and Streams cs784(Prasad) L11Clos
Binomial Priority Queues
Nondeterministic Evaluation
6.001 SICP Data abstractions
Streams Sections 3.5.1,3.5.2 Pages
The Metacircular Evaluator
Lecture 18 Infinite Streams and
PPL Sequence Interface.
Lecture 18.
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 13 - Assignment and the environments model Chapter 3
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
topics mutable data structures
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Lecture # , , , , מבוא מורחב.
Binomial Priority Queues
list data list 만들기 list 사용하기 nil : list link :  * list -> list
Lecture 8: Recursing Lists CS150: Computer Science
Presentation transcript:

מבוא מורחב 1 Lecture #8

מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble (list )) ==> ( ) (scramble (list )) ==> ( ) (scramble (list )) ==> ( )

מבוא מורחב 3 Ok ok Each number in the argument is treated as backword index from its own position to a point earlier in the tup. The result at each position is found by counting backward from the current position according to this index ==> No number can be greater than its index

מבוא מורחב 4 tup = ( ) rev-pre = () (cons 0 …. tup = (1 2 1) rev-pre = (0) (cons 0 (cons 0 tup = (2 1) rev-pre = (1 0) (cons 0 (cons 0 (cons 0 tup = (1) rev-pre = (2 1 0) (cons 0 (cons 0 (cons 0 (cons 2 tup = ( ) rev-pre = ( ) (cons 0 (cons 0 (cons 0 (cons 2 ()))))

מבוא מורחב 5 list-ref scramble-i (define (scramble-i tup rev-pre) (if (null? tup) '() (cons ( (cons (car tup) rev-pre) (car tup)) ( (cdr tup) (cons (car tup) rev-pre))))) (define (scramble tup) (scramble-b tup '()))

מבוא מורחב 6 Extracting common patterns: High order procedures to handle lists

מבוא מורחב 7 Transforming a List (map) (define (square-list lst) (if (null? lst) nil (cons (square (car lst)) (square-list (cdr lst))))) (define (double-list lst) (if (null? lst) nil (cons (* 2 (car lst)) (double-list (cdr lst))))) (define (map proc lst) (if (null? lst) nil (cons (proc (car lst)) (map proc (cdr lst))))) (define (square-list lst) (map square lst)) (define (double-list lst) (map (lambda (x) (* 2 x)) lst)) define (scale-list lst f) (map (lambda (x) (* f x)) lst)) (scale-list 1-to-4 10) ==> ( )

מבוא מורחב 8 Pick odd elements out of a list (define (odds lst) (cond ((null? lst) nil) ((odd? (car lst)) (cons (car lst) (odds (cdr lst)))) (else (odds (cdr lst))))) (odds 1-to-4) ==> (1 3)

מבוא מורחב 9 Filtering a List (filter) (define (filter pred lst) (cond ((null? lst) nil) ((pred (car lst)) (cons (car lst) (filter pred (cdr lst)))) (else (filter pred (cdr lst))))) (filter odd? 1-to-4) ==> (1 3) (filter even? 1-to-4) ==> (2 4)

מבוא מורחב 10 More examples In tirgul: (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)))))) ( map fib (integers-between 10 20)) ==> (55 89 …. 6765) (filter even? (map fib (integers-between 10 20))) (map fib (filter even? (integers-between 10 20)))

מבוא מורחב 11 Accumulating Results (accumulate) (define (add-up lst) (if (null? lst) 0 (+ (car lst) (add-up (cdr lst))))) (define (mult-all lst) (if (null? lst) 1 (* (car lst) (mult-all (cdr lst))))) (define (accumulate op init lst) (if (null? lst) init (op (car lst) (accumulate op init (cdr lst)))))

מבוא מורחב 12 Accumulating (cont.) (define (accumulate op init lst) (if (null? lst) init (op (car lst) (accumulate op init (cdr lst))))) (define (add-up lst) (accumulate + 0 lst)) el n init op el n-1 el 1 …….. op...

מבוא מורחב 13 Length as accumulation (define (length lst) (if (null? lst) 0 (+ 1 (length (cdr lst))))) (define (accumulate op init lst) (if (null? lst) init (op (car lst) (accumulate op init (cdr lst))))) (define (length lst) (accumulate (lambda (x y) (+ 1 y))) 0 lst))

מבוא מורחב 14 Append as accumulation (define (append list1 list2) (if (null? list1) list2 (cons (car list1) (append (cdr list1) list2)))) (define (accumulate op init lst) (if (null? lst) init (op (car lst) (accumulate op init (cdr lst))))) Rewrite append as an accumulation (define (append lst1 lst2) (accumulate cons lst2 lst1)

מבוא מורחב 15 What did we gain ? U sing our tools: (define (easy lo hi) (accumulate * 1 (map fib (filter even? (integers-between lo hi))))) Without: (define (hard lo hi) (cond ((> lo hi) 1) ((even? lo) (* (fib lo) (hard (+lo 1) hi))) (else (hard (+ lo 1) hi))))

מבוא מורחב 16 Finding all the primes

מבוא מורחב 17.. And here’s how to do it! (define (sieve lst) (if (null? lst) nil (cons (car lst) (sieve (filter (lambda (x) (not (divisible? x (car lst)))) (cdr lst)))))) (sieve (integers-between 2 420)) ( )

מבוא מורחב 18 View a list as a tree

מבוא מורחב 19 Trees root children or subtrees (define tree (list 2 (list 6 8) 4)) We can view a list of possibly other lists and atoms as a tree. (length tree) ==> (countleaves tree) ==> 3 4

מבוא מורחב 20 countleaves Strategy base case: count of an empty tree is 0 base case: count of a leaf is 1 recursive strategy: the count of a tree is the sum of the countleaves of each child in the tree. Implementation: (define (countleaves tree) (cond ((null? tree) 0) ;base case ((leaf? tree) 1) ;base case (else ;recursive case (+ (countleaves (car tree)) (countleaves (cdr tree)))))) (define (leaf? x) (not (pair? x)))

מבוא מורחב 21 countleaves and substitution model – pg. 2 (define (countleaves tree) (cond ((null? tree) 0) ;base case ((leaf? tree) 1) ;base case (else ;recursive case (+ (countleaves (car tree)) (countleaves (cdr tree)))))) (define (leaf? x) (not (pair? x))) Example #2 (countleaves (list 5 7)) (countleaves ) (countleaves (5 7) ) (+ (countleaves 5) (countleaves (7) )) (+ 1 (+ (countleaves 7) (countleaves nil))) (+ 1 (+ 1 0)) ==> 2 7 5

22 countleaves – bigger example (define my-tree (list 4 (list 5 7) 2)) (countleaves my-tree) (countleaves (4 (5 7) 2) ) (+ (countleaves 4) (countleaves ((5 7) 2) )) ==> my-tree (cl (4 (5 7) 2)) + (cl 4)(cl ((5 7) 2) ) + (cl (5 7))(cl (2)) + (cl 2) (cl nil) + (cl 5) (cl (7)) + (cl 7) (cl nil)

מבוא מורחב 23 Your Turn: scale-tree Goal: given a tree, produce a new tree with all the leaves scaled Strategy base case: scale of empty tree is empty tree base case: scale of a leaf is product otherwise, recursive strategy: build a new tree from a scaled version of the first child and a scaled version of the rest of children Implementation: (define (scale-tree tree factor) (cond ((null? tree) nil) ;base case ((leaf? tree) (* tree factor)) (else ;recursive case (cons (scale-tree (cdr tree) ))))) (scale-tree (car tree) factor) factor

24 Alternative scale-tree Strategy base case: scale of empty tree is empty tree base case: scale of a leaf is product otherwise: build a new tree from a scaled version of the children recognize that a tree is a list of subtrees and use a list oriented HOP! Alternative Implementation using map: (define (scale-tree tree factor) (cond ((null? tree) nil) ((leaf? tree) (* tree factor)) (else ;it’s a list of subtrees (map (lambda (child) (scale-tree child factor)) tree))))

מבוא מורחב 25 Lists as interfaces

מבוא מורחב 26 (define (sum-odd-squares tree) (cond ((null? tree) 0) ((leaf? tree) (if (odd? tree) (square tree) 0)) (else (+ (sum-odd-squares (car tree)) (sum-odd-squares (cdr tree)))))) (define (even-fibs n) (define (next k) (if (< k n) nil (let ((f (fib k))) (if (even? f) (cons f (next (+ k 1))) (next (+ k 1)))))) (next 0))

27 Sum-odd-squares enumerates the leaves of a tree filters them, selecting the odd ones squares each of the selected ones accumulate the results using + Even-fibs enumerates the integers from 0 to n computes the Fibonacci number for each integer filters them selecting the even ones accumulated the results using cons

מבוא מורחב 28 enumerate leaves filter map accumulate tree odd? square + 0 integers between map filter accumulate 0, n fib even? consnil even-fibs sum-odd-squares On horizontal arrows flow lists of numbers (interface)

29 Implementation (define (sum-odd-squares tree) (accumulate + 0 (map square (filter odd? (enumerate-tree tree))))) (define (even-fibs n) (accumulate cons nil (filter even? (map fib (integers-between 0 n))))) (define (enumerate-tree tree) (cond ((null? tree) nil) ((leaf? tree) (list tree)) (else (append (enumerate-tree (car tree)) (enumerate-tree (cdr tree))))))

מבוא מורחב 30 How does the interpreter prints lists and pairs ??

מבוא מורחב 31 First version, using dot notation (define (print-list-structure x) (define (print-contents x) (print-list-structure (car x)) (display ". ") (print-list-structure (cdr x))) (cond ((null? x) (display "()")) ((atom? x) (display x)) (else (display "(") (print-contents x) (display ")")))) (p-l-s (list 1 2 3)) ==> (1. (2. (3. ())))

32 Second version, try to identify lists (define (print-list-structure x) (define (print-contents x) (print-list-structure (car x)) (cond ((null? (cdr x)) nil) ((atom? (cdr x)) (display ". ") (print-list-structure (cdr x))) (else (display " ") (print-contents (cdr x))))) (cond ((null? x) (display "()")) ((atom? x) (display x)) (else (display "(") (print-contents x) (display ")")))) (p-l-s (list 1 2 3)) ==> (1 2 3)

מבוא מורחב 33 More examples How to create the following output ? ( ) (cons 1 (cons 2 3)) (1. 2 3) cannot