מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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)
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Let* List and pairs manipulations –Insertion Sort Abstraction Barriers –Fractals –Mobile 2.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Data directed programming Section 2.4, pages ,2.5.2 pages (but with a different example)
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
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.
מבוא מורחב 1 Lecture #13. מבוא מורחב 2 Multiple representations of data.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Abstraction Barriers –Fractals –Mobile List and pairs manipulations –Insertion Sort 2.
מבוא מורחב למדעי המחשב בשפת 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.
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.
1 Lecture 15: More about assignment and the Environment Model (EM)
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
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.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
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.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Fall 2008Programming Development Techniques 1 Topic 5 Data Abstraction Note: This represents a change in order. We are skipping to chapter 2 without finishing.
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/32 Data Mutation Primitive and compound data mutators set! for names set-car!, set-cdr! for pairs Stack example non-mutating mutating Queue example non-mutating.
מבוא מורחב למדעי המחשב בשפת 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)
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Additional Scheme examples
Edited by Original material by Eric Grimson
CS 550 Programming Languages Jeremy Johnson
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
6.001 SICP Data abstractions
The Metacircular Evaluator
Lecture 15: Tables and OOP
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.
Lecture #8 מבוא מורחב.
The Metacircular Evaluator (Continued)
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
Lecture #6 section pages pages72-77
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
6.001 SICP Data abstractions
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
6.001: Structure and Interpretation of Computer Programs
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Lists in Lisp and Scheme
Presentation transcript:

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

מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator of (denom ) Returns the denominator of Constructor: Selectors:

מבוא מורחב 3 A contract (numer (make-rat )) (denom (make-rat )) = How do we do it ?

מבוא מורחב 4 Pairs (cons cells) (cons ) ==> ;type: x, x  Pair Where evaluates to a value, and evaluates to a value Returns a pair whose car-part is and whose cdr-part is (car ) ==> ;type: Pair  type of car part Returns the car-part of the pair (cdr ) ==> ;type: Pair  type of cdr part Returns the cdr-part of the pair

מבוא מורחב 5 Using pairs (define (make-rat n d) (cons n d)) (define (numer x) (car x)) (define (denom x) (cdr x))

מבוא מורחב 6 Alternative implementation (define (add-rat x y) (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) (define (add-rat x y) (cons (+ (* (car x) (cdr y)) (* (car y) (cdr x))) (* (cdr x) (cdr y)))) Abstraction Violation

מבוא מורחב 7 Reducing to lowest terms (define (make-rat n d) (let ((g (gcd n d))) (cons (/ n g) (/ d g))))

מבוא מורחב 8 Abstraction barriers Programs that use rational numbers add-rat sub-rat ……. make-rat numer denom car cdr cons

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

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

מבוא מורחב 11 In the book: (define (cons x y) (define (dispatch m) (cond ((= m 0) x) ((= m 1) y) (else (error "Argument not 0 or 1 -- CONS" m)))) dispatch) (define (car z) (z 0)) (define (cdr z) (z 1))

מבוא מורחב 12 Compound data Ideally want the result of this “gluing” to have the property of closure: “the result obtained by creating a compound data structure can itself be treated as a primitive object and thus be input to the creation of another compound object”

מבוא מורחב 13 Box and pointer diagram Note how pairs have the property of closure – we can use the result of a pair as an element of a new pair: (cons (cons 1 2) 3) 32 1

מבוא מורחב 14 Box and pointer diagram (cons (cons 1 (cons 2 3)) 4)

מבוא מורחב 15 Lists (cons 1 (cons 3 (cons 2 ‘())))

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

מבוא מורחב 17 Lists (list... ) Same as (cons (cons (cons … (cons ‘())))) …

מבוא מורחב 18 Lists Predicates ; null? anytype -> boolean (null? ) ==> #t if evaluates to empty list ; pair? anytype -> boolean (pair? ) ==> #t if evaluates to a pair ; atom? anytype -> boolean (define (atom? z) (and (not (pair? z)) (not (null? z))))

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

מבוא מורחב 20 Common Pattern #1: cons’ing up a list (define (enumerate-interval from to) (if (> from to) nil (cons from (enumerate-interval (+ 1 from) to)))) (e-i 2 4) (if (> 2 4) nil (cons 2 (e-i (+ 1 2) 4))) (if #f nil (cons 2 (e-i 3 4))) (cons 2 (e-i 3 4)) (cons 2 (cons 3 (e-i 4 4))) (cons 2 (cons 3 (cons 4 (e-i 5 4)))) (cons 2 (cons 3 (cons 4 nil))) (cons 2 (cons 3 )) (cons 2 ) ==> (2 3 4)

מבוא מורחב 21 Common Pattern #2: cdr’ing down a list (define (list-ref lst n) (if (= n 0) (car lst) (list-ref (cdr lst) (- n 1)))) (define squares (list )) (list-ref squares 3) ==> 16

מבוא מורחב 22 Common Pattern #2: cdr’ing down a list (define (length lst) (if (null? lst) 0 (+ 1 (length (cdr lst))))) (define odds (list )) (length odds) ==> 4

מבוא מורחב 23 Iterative length (define (length items) (define (length-iter a count) (if (null? a) count (length-iter (cdr a) (+ 1 count)))) (length-iter items 0))

24 Cdr’ing and Cons’ing Examples (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))) ==> ( ) T(n) =  (n)

25 Cdr’ing and Cons’ing Examples (define (copy lst) (if (null? lst) nil ; base case (cons (car lst) ; recursion (copy (cdr lst))))) (append (list 1 2) (list 3 4)) ==> ( ) Strategy: “copy” list1 onto front of list2. (define (append list1 list2) (cond ((null? list1) list2) ; base (else (cons (car list1) ; recursion (append (cdr list1) list2)))))

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

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

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

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

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