Practice session 5 טיפוסים מורכבים Abstract Data Types הכנה לעבודה 3.

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

Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
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)
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
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
CSI2520  Un arbre binaire peut être représentée avec des listes imbriquées a / \ b c / \ d e (a b (c d e)) ou (a (b () ()) (c (d () ()) (e () ())) ou.
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.
Creating Functional Programs Brad Vander Zanden. Basic Techniques Tail Recursion – Use continuation arguments if necessary – Akin to pre-processing a.
מבוא מורחב - שיעור 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:
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
Scheme for Python Programmers CSE Valeria Montero.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 25: Trees and Generalized Lists.
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)))
תכנות תרגול 12 שבוע : מבנים מטרת המבנים היא לאפשר למתכנת להגדיר טיפוסי משתנים חדשים אשר מתאימים ספציפית לבעיה שאותה התוכנית פותרת. מטרת המבנים.
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
PPL Pairs, lists and data abstraction. Compound Data Until now: atomic, unrelated entities Now: organized into structures Why? – Better conceptual level.
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.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
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
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
Spring 2004Programming Development Techniques 1 Topic 11 Sets and their Representation April 2004.
Practice session 6 Sequence Operations Partial Evaluation Lazy Lists.
מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real i 13 atan(2/3)
Functional Programming: Lisp MacLennan Chapter 10.
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 Representing lists as binary trees
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.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live.
Additional Scheme examples
CS 550 Programming Languages Jeremy Johnson
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
CS 270 Math Foundations of CS Jeremy Johnson
CSC 533: Organization of Programming Languages Spring 2008
COP4020 Programming Languages
Lecture 18 Infinite Streams and
PPL Sequence Interface.
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Lecture 18.
מבוא לתכנות ב- Java תרגול 10 - רשימות מקושרות.
Lecture #8 מבוא מורחב.
Chapter 4 Data and Behavior Abstraction
topics mutable data structures
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
6.001 SICP Data abstractions
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
Functional Programming: Lisp
List Allocation and Garbage Collection
Lecture # , , , , מבוא מורחב.
CSC 533: Organization of Programming Languages Spring 2007
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Practice session 5 טיפוסים מורכבים Abstract Data Types הכנה לעבודה 3

 מייצג זוג סדור של ערכים מכל טיפוס שהוא  בנאי : cons Type: [T1 * T2 -> PAIR(T1, T2)]  צורת ה -"toString" של Pair: ‘(a. b)  סלקטורים : car,cdr Type: [PAIR(T1, T2) -> T1] Type: [PAIR(T1, T2) -> T2]

> (cons 1 2) ' (2. 1) >(cons 1 (cons 2 (lambda () (+ 1 2)))) '(1 2. # ) (1. (2. # )) >(cons (car (cons 1 2)) (cdr (cons 3 4))) '(4. 1) >(cons (cons 1 2) (cons 1 3)) ((3. 1) 2. 1)'

>(define c (cons 1 (cons 'cat (cons 3 (cons 4 5))))) > c '(1 cat ) > (cdddr c) '(4. 5) > (cdr (cdr (cdr c))) '(4. 5) > (cadr c) 'cat > (car (cdr c)) 'cat

 מייצגת סדרה סדורה של ערכים מכל טיפוס שהוא  הגדרה רקורסיבית : רשימה היא זוג של איבר ראשון ורשימת שאר האיברים, כאשר הרשימה הריקה היא האיבר האחרון.  שני בנאים : cons, list  מבחינים בין רשימה הומוגנית List(T) ורשימה הטרוגנית LIST cons: Type: [T * List(T) -> List(T)] Type: [T * LIST -> LIST] Type: [T1 * List (T2) -> LIST], T1≠T2 list: Type: [T * T *... * T -> List(T)] Type: [T1 * T2 *... * Tn -> LIST]

 סלקטורים : car,cdr car: Type: [LIST(T) -> T] Type: [LIST -> T] cdr: Type: [LIST(T) -> LIST(T)] Type: [LIST -> LIST]  list?, null?, equal?, append, length

> (define a (list 3 4 5)) > a (5 4 3)' > (cons 1 a) '( ) > (list) '() > (list (list 1 2) 1 2) '((1 2) 1 2)

> (define L2 (list 1 (list 2 3) 6)) > L2 '(1 (2 3) 6) > (car L2) 1 > (cdddr L2) '() > (list? (cons (list 2 3) 4)) #f > (list? (cons 4 (list 2 3))) #t

רוצים לכתוב פרוצדורת append שמקבלת שתי רשימות ומחברת אותן לרשימה אחת, ופרוצדורת flatten שמקבלת רשימה ו " משטחת " אותה. > (append (list 1 2 3) (list 4 5 6)) '( ) > (flatten (list 1 2 (list 3 (list) 4) 5 (list 6))) '( )

Signature: append (list1, list2) Purpose: return a list which appends the arguments lists, from left to right. Type: [LIST * LIST -> LIST] (define append (lambda (x y) (if (null? x) y (cons (car x) (append (cdr x) y)))))

Signature: flatten(list) Purpose: return a list of all argument "leaves". Type: [LIST -> LIST] (define flatten (lambda (x) (cond ((null? x) '()) ((not (pair? x)) (list x)) (else (append (flatten (car x)) (flatten (cdr x)))))))