Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section 2.2.1 September 2008.

Slides:



Advertisements
Similar presentations
ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
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.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
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)
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.
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.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, pm GPB.
מבוא מורחב 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)))
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
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.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Functional Programming 02 Lists
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
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.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
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.
CS61A Lecture Colleen Lewis. Clicker poll Where do you think you’re learning the most? I assume you’ll learn the most in lab & disc (so.
מבוא מורחב שיעור 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.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
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
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
Racket CSC270 Pepper major portions credited to
The Environment Model*
Chapter 15 – Functional Programming Languages
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
COP4020 Programming Languages
6.001 SICP Data abstractions
Lecture #8 מבוא מורחב.
Modern Programming Languages Lecture 20 Fakhar Lodhi
Lecture #9 מבוא מורחב.
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).
Defining Functions with DEFUN
Today’s topics Abstractions Procedural Data
Functional Programming: Lisp
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
Lisp.
More Scheme CS 331.
Defining Macros in Scheme
Lists in Lisp and Scheme
Presentation transcript:

Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008

Spring 2008Programming Development Techniques 2 Box and pointer notation Draw cdr pointers to the right Draw car pointers downward (cons 1 2) 2 1

Spring 2008Programming Development Techniques 3 Another list structure (cons (cons 1 2) (cons 3 4))

Spring 2008Programming Development Techniques 4 The closure property A constructor has the closure property if it can take data of a certain type as input and return data of the same type cons is an example Such constructors can be used to build hiearchical structures

Spring 2008Programming Development Techniques 5 Lists, a recursive data type The empty list is a list If x is any datum and y is a list, then (cons x y) is a list The empty list is denoted by empty in DrScheme and by nil in the course textbook Whenever you see nil in the book, read empty

Spring 2008Programming Development Techniques 6 What do lists look like? (cons 1 ( cons 2 (cons 3 (cons 4 empty)))) ( )

Spring 2008Programming Development Techniques 7 Lists can contain lists (cons 1 (cons (cons 2 (cons 3 empty)) (cons 4 (cons (cons 5 (cons 6 empty)) empty)))) (1 (2 3) 4 (5 6))

Spring 2008Programming Development Techniques 8 Box and pointer representation

Spring 2008Programming Development Techniques 9 Printing out list structures Printed like lists, but if the last cdr in a cdr chain points to a primitive datum other than empty, the primitive datum is printed with a dot in front of it.

Spring 2008Programming Development Techniques 10 A comparison ( ) ( )

Spring 2008Programming Development Techniques 11 Some service procedures for lists (list ) --> ( ) ;; takes a list with at least n elements ;; and returns the nth element of the list ;; note counting starts from 0 (define (our-list-ref lst n) (if (= n 0) (car lst) (our-list-ref (cdr lst) (- n 1)))) (our-list-ref (list ) 0) --> 1 (our-list-ref (list ) 2) --> 3

Spring 2008Programming Development Techniques 12 More service procedures (define (null? x) (equal? x empty)) ; takes a list and returns the number ; of elements in the list (define (our-length list) (if (null? list) 0 (+ 1 (our-length (cdr list)))))

Spring 2008Programming Development Techniques 13 our-member ; takes an element and a list and returns non-#f if ; ele is in the list (define (our-member ele lst) (cond ((null? lst) #f) ((equal? ele (car lst)) lst) (else (our-member ele (cdr lst)))))

Spring 2008Programming Development Techniques 14 (first-n lst n)

Spring 2008Programming Development Techniques 15 Append ; takes two lists and returns a list ; containing the elements of the ; original 2 (define (our-append list1 list2) (if (null? list1) list2 (cons (car list1) (our-append (cdr list1) list2)))) (our-append (list 1 2) (list 3 4)) --> ( )

Spring 2008Programming Development Techniques 16 Notes Procedures list-ref, null?, eq?, length, and append are predefined procedures in Scheme Procedure append can append any number of lists together Procedure pair? returns #t if its argument is a pair, else #f