Lecture #7 90-92, 2.1.3 99-103, 2.2.1 105-107, 2.2.1 107-112, 2.2.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

Programming with Lists
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Let* List and pairs manipulations –Insertion Sort Abstraction Barriers –Fractals –Mobile 2.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
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:
מבוא מורחב למדעי המחשב בשפת 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.
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:
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Abstraction Barriers –Fractals –Mobile List and pairs manipulations –Insertion Sort 2.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
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)))
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
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 You’re Invited! Course VI Freshman Open House! Friday, April 7, :30-5:00 PM FREE Course VI T-Shirts (while supplies last) and Department.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
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.
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.
מבוא מורחב למדעי המחשב בשפת 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)
Additional Scheme examples
CS 550 Programming Languages Jeremy Johnson
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
6.001 SICP Variations on a Scheme
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
Racket CSC270 Pepper major portions credited to
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Closures and Streams cs784(Prasad) L11Clos
CS 270 Math Foundations of CS Jeremy Johnson
COP4020 Programming Languages
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 #9 מבוא מורחב.
Lecture 13 - Assignment and the environments model Chapter 3
topics mutable data structures
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
list data list 만들기 list 사용하기 nil : list link :  * list -> list
Presentation transcript:

Lecture #7 90-92, 2.1.3 99-103, 2.2.1 105-107, 2.2.1 107-112, 2.2.2 מבוא מורחב

How can we implement pairs ? (define (cons x y) (lambda (m) (cond ((= m 0) x) ((= m 1) y) (else (error "Argument not 0 or 1 -- CONS" m)))))) (define (car z) (z 0)) (define (cdr z) (z 1)) מבוא מורחב

The pair (cons 6 9) The pair constructed by (cons 6 9) is the procedure: (lambda (m) (cond ((= m 0) 6) ((= m 1) 9) (else (error “Argument not 0 or 1 -- CONS” m)))) מבוא מורחב

… Lists (list <x1> <x2> ... <xn>) Same as (cons <x1> (cons <x2> ( … (cons <xn> nil)))) <x1> <x2> <xn> … מבוא מורחב

lists A list is either ‘() (The empty list) A pair whose cdr is a list. Note that lists are closed under operations of cons and cdr. מבוא מורחב

Null? (null? (list 1))  #f (null? (cdr (list 1)))  #t null? : anytype -> boolean (null? <z>) #t if <z> evaluates to empty list #f otherwise (null? 2)  #f (null? (list 1))  #f (null? (cdr (list 1)))  #t (null? ‘())  #t מבוא מורחב

pair? pair? : anytype -> boolean (pair? <z>) #t if <z> evaluates to a pair #f otherwise. (pair? (cons 1 2))  #t (pair? (cons 1 (cons 1 2)))  #t (pair? (list 1))  #t (pair? ‘())  #f מבוא מורחב

atom? atom? : anytype -> boolean (define (atom? z) (and (not (pair? z)) (not (null? z)))) (define (sqrt x) (* x )) (atom? sqrt)  #t Not a primitive procedure מבוא מורחב

caddr (define one-to-four (list 1 2 3 4)) one-to-four ==> (1 2 3 4) (1 2 3 4) ==> error (car one-to-four) ==> 1 (car (cdr one-to-four)) ==> 2 (cadr one-to-four) ==> 2 (caddr one-to-four) ==> 3 מבוא מורחב

Common Pattern #1: cons’ing up a list (define (enumerate-primes from to) (cond ((> from to) '()) ((prime? from) (cons from (enumerate-primes (+ 1 from) to))) (else (enumerate-primes (+ 1 from) to)))) (enumerate-primes 2 5) (cons 2 (enumerate-primes (+ 1 2) 4))) (cons 2 (cons 3 (enumerate-primes 4 5))) (cons 2 (cons 3 (enumerate-primes 5 5))) (cons 2 (cons 3 (cons 5 (enumerate-primes 6 5)))) (cons 2 (cons 3 (cons 5 ‘()))) (list 2 3 5) 2 3 5 מבוא מורחב

Common Pattern #1: enumerate-squares (define (enumerate-squares from to) (cond ((> from to) '()) (else (cons (square from) (enumerate-squares (+ 1 from) to))))) (enumerate-squares 2 4) (cons 4 (enumerate-squares 3 4))) (cons 4 (cons 9 (enumerate-squares 4 4))) (cons 4 (cons 9 (cons 16 (enumerate-squares 5 4)))) (cons 4 (cons 9 (cons 16 ‘()))) (list 4 9 16) 4 9 16 מבוא מורחב

Common Pattern #2: cdr’ing down a list (define (length lst) (if (null? lst) (+ 1 (length (cdr lst))))) (length (list 1 2 3))  3 (length (enumerate-squares 1 100))  100 (length (enumerate-primes 1 1000))  169 (length (enumerate-primes 1001 2000))  135 (length (enumerate-primes 100001 101000))  81 (length (enumerate-primes 10000001 10001000))  61 מבוא מורחב

Length – iterative version (define (length lst) (define (length-iter temp count) (if (null? temp) count (length-iter (cdr temp) (+ 1 count)))) (length-iter lst 0)) מבוא מורחב

Another example: cdr’ing down a list (define (list-ref lst n) ; find n’th element (if (= n 1) (car lst) (list-ref (cdr lst) (- n 1)))) (list-ref (list 1 2 3) 1)  1 (list-ref (list 1 2 3) 4)  car: expects argument of type <pair>; given () (define list-of-primes (enumerate-primes 2 10000)) (list-ref list-of-primes 57)  269 מבוא מורחב

Append (define (append list1 list2) (cond ((null? list1) list2) ; base (else (cons (car list1) ; recursion (append (cdr list1)list2))))) (append (list 1 2) (list 3 4)) (cons 1 (append (2) (3 4))) (cons 1 (cons 2 (append () (3 4)))) (cons 1 (cons 2 (3 4))) (1 2 3 4) Time complexity: T(n) = (n) car,cdr,cons operations.

Reverse (define (reverse lst) (cond ((null? lst) lst) (else (append (reverse (cdr lst)) (list (car lst))))))) (reverse (list 1 2 3 4)) ==> (4 3 2 1) (reverse (list 1 2 3)) (append (reverse (2 3)) (1)) (append (append (reverse (3)) (2)) (1)) (append (append (append (reverse ()) (3)) (2)) (1)) (append (append (append () (3)) (2)) (1)) (append (append (3) (2)) (1)) (append (3 2) (1)) (3 2 1) Append: T(n) = c*n = (n) Reverse: T(n) = c*(n-1) + c*(n-2) … c*1 = (n2)

Square-list, double-list, (define (square-list lst) (if (null? lst) ‘() (cons (square (car lst)) (square-list (cdr lst))))) (define (double-list lst) (if (null? lst) ‘() (cons (* 2 (car lst)) (double-list (cdr lst))))) מבוא מורחב

High order procedures to handle lists: map (define (map proc lst) (if (null? lst) ‘() (cons (proc (car lst)) (map proc (cdr lst))))) (define (square-list lst) (map square lst)) (define (scale-list lst c) (map (lambda (x) (* c x)) lst)) (scale-list (list 1 2 3 5) 10) ==> (10 20 30 50) מבוא מורחב

Pick odd elements out of a list (define (odd-elements lst) (cond ((null? lst) ‘()) ((odd? (car lst)) (cons (car lst) (odd-elements (cdr lst)))) (else (odd-elements (cdr lst))))) (enumerate-squares 2 6)  (4 9 16 25 36) (odd-elements (enumerate-squares 2 6))  (9 25) מבוא מורחב

Filtering a List (filter) (define (filter pred lst) (cond ((null? lst) ‘()) ((pred (car lst)) (cons (car lst) (filter pred (cdr lst)))) (else (filter pred (cdr lst))))) (define list-of-squares (enumerate-squares 1 10)) (filter odd? list-of-squares) (1 9 25 49 81) מבוא מורחב

Accumulating Results (accumulate) (define (add-up lst) (if (null? lst) (+ (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))))) מבוא מורחב

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

Length and append as accumulation (define (length lst) (accumulate (lambda (x y) (+ 1 y))) lst)) (define (append lst1 lst2) (accumulate cons lst2 lst1) מבוא מורחב

Finding all the primes 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 60 69 68 67 66 65 64 63 62 61 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 XX X 3 XX 7 XX X 2 XX 5 מבוא מורחב

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

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

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))) מבוא מורחב

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

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 מבוא מורחב

Scale-tree (define (scale-tree tree factor) (cond ((null? tree) nil) ;base case ((leaf? tree) (* tree factor)) (else ;recursive case (cons (scale-tree (cdr tree) factor ))))) (scale-tree (car tree) factor) מבוא מורחב

Alternative scale-tree Strategy base case: scale of empty tree is empty tree base case: scale of a leaf is product otherwise: a tree is a list of subtrees and use 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))))