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.

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.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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)
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)
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
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.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Lisp Internals. 2 A problem with lists In Lisp, a list may “contain” another list –For example, (A (B C)) contains (B C) So, how much storage do we need.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
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.
Programming Languages I LISP
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
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.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Lisp by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
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.
Functional Programming 02 Lists
PRACTICAL COMMON LISP Peter Seibel 1.
PRACTICAL COMMON LISP Peter Seibel 1.
CSE S. Tanimoto Lisp Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Functional Programming in Scheme and Lisp.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
You can access the members of a list with the functions car (or first) and cdr (or rest): (setf list '(a b c)) (car list) ⇒ a (first list) ⇒ a (cdr list)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Comparative Programming Languages Functional programming with Lisp/Scheme.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Lisp S-Expressions: ATOMs
CS 550 Programming Languages Jeremy Johnson
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
LISP A brief overview.
J.E. Spragg Mitthögskolan 1997
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Scheme: Basic Functionality
Lisp: Representation of Data
Lecture #7 מבוא מורחב.
Defining Functions with DEFUN
Abstraction and Repetition
LISP: Basic Functionality
LISP: Basic Functionality
Common Lisp II.
LISP: Basic Functionality
Lisp.
Lisp: Representation of Data
LISP primitives on sequences
Lists in Lisp and Scheme
Presentation transcript:

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 data structure. However, it is not the only data structure. There are arrays, characters, strings, etc. There are arrays, characters, strings, etc. Common Lisp has moved on from being merely a LISt Processor. Common Lisp has moved on from being merely a LISt Processor. However, to understand Lisp and Scheme you must understand lists However, to understand Lisp and Scheme you must understand lists common functions on them common functions on them how to build other useful data structures using them. how to build other useful data structures using them.

3 In the beginning was the cons (or pair) What cons really does is combines two objects into a two-part object called a cons in Lisp and a pair in Scheme What cons really does is combines two objects into a two-part object called a cons in Lisp and a pair in Scheme Conceptually, a cons is a pair of pointers -- the first is the car, and the second is the cdr. Conceptually, a cons is a pair of pointers -- the first is the car, and the second is the cdr. Conses provide a convenient representation for pairs of any type. Conses provide a convenient representation for pairs of any type. The two halves of a cons can point to any kind of object, including conses. The two halves of a cons can point to any kind of object, including conses. This is the mechanism for building lists. This is the mechanism for building lists. (pair? ‘(1 2) ) => T (pair? ‘(1 2) ) => T nil a

4 Pairs Lists in CL and Scheme are defined as pairs. Lists in CL and Scheme are defined as pairs. Any non empty list can be considered as a pair of the first element and the rest of the list. Any non empty list can be considered as a pair of the first element and the rest of the list. We use one half of the cons to point to the first element of the list, and the other to point to the rest of the list (which is either another cons or nil). We use one half of the cons to point to the first element of the list, and the other to point to the rest of the list (which is either another cons or nil). nil a

5 Box notation nil a A one element list (A) nil ab c A list of 3 elements (A B C) Where NIL = ‘( )

6 What sort of list is this? nil a d b c > (set! z (list ‘a (list ‘b ‘c) ‘d)) (A (B C) D)  (car (cdr z))  ?? Z

7 Pair? The function pair? returns true if its argument is a cons (pair? = consp in CL) The function pair? returns true if its argument is a cons (pair? = consp in CL) So list? could be defined: So list? could be defined: (defun myListp (x) (or (null x) (consp x))) (defun myListp (x) (or (null x) (consp x))) Since everything that is not a pair is an atom, the predicate atom could be defined: Since everything that is not a pair is an atom, the predicate atom could be defined: (defun myAtom (x) (not (pair? x))) (defun myAtom (x) (not (pair? x))) Remember, nil is both an atom and a list. Remember, nil is both an atom and a list.

8 Equality Each time you call cons, Scheme allocates a new piece of memory with room for 2 pointers. Each time you call cons, Scheme allocates a new piece of memory with room for 2 pointers. So if we call cons twice with the same arguments, we get back two values that look the same, but are in fact distinct objects: So if we call cons twice with the same arguments, we get back two values that look the same, but are in fact distinct objects: > (define l1 (cons 'a ‘())) (A)  (define l2 (cons 'a nil)))  (A)  (eq? l1 l2)  #f  (equal l1 l2)  #t  (and (eq? (car l1)(car l2)) (eq? (cdr l1)(cdr l2)))  #t

9 Equal? We also need to be able to ask whether two lists have the same elements. We also need to be able to ask whether two lists have the same elements. CL provides an equality predicate for this, equal. CL provides an equality predicate for this, equal. Eq? returns true only if its arguments are the same object, and Eq? returns true only if its arguments are the same object, and equal, more or less, returns true if its arguments would print the same. equal, more or less, returns true if its arguments would print the same. > (equal? l1 l2) T Note: if x and y are eq?, they are also equal?

10 Equal? (define (equal? x y) ; this is ~ how equal? could be defined ; this is ~ how equal? could be defined (cond ((number? x) (= x y)) (cond ((number? x) (= x y)) ((not (pair? x)) (eq? x y)) ((not (pair? x)) (eq? x y)) ((not (pair? y)) nil) ((not (pair? y)) nil) ((equal (car x) (car y)) ((equal (car x) (car y)) (equal (cdr x) (cdr y))))) (equal (cdr x) (cdr y)))))

11 Does Lisp have pointers? One of the secrets to understanding Lisp is to realize that variables have values in the same way that lists have elements. One of the secrets to understanding Lisp is to realize that variables have values in the same way that lists have elements. As conses have pointers to their elements, variables have pointers to their values. As conses have pointers to their elements, variables have pointers to their values. What happens, for example, when we set two variables to the same list: What happens, for example, when we set two variables to the same list: > (set! x ‘(a b c)) (A B C) > (set! y x) (A B C) This is just like in Java This is just like in Java

12 The location in memory associated with the variable x does not contain the list itself, but a pointer to it. The location in memory associated with the variable x does not contain the list itself, but a pointer to it. When we assign the same value to y, Sccheme copies the pointer, not the list. When we assign the same value to y, Sccheme copies the pointer, not the list. Therefore, what would the value of Therefore, what would the value of > (eq? x y) be, #t or #f? Does Scheme have pointers?

13 Building Lists The function copy-list takes a list and returns a copy of it. The function copy-list takes a list and returns a copy of it. The new list will have the same elements, but contained in new conses. The new list will have the same elements, but contained in new conses. > (set! x ‘(a b c)) (A B C) > (set! y (copy-list x)) (A B C) Spend a few minutes to draw a box diagram of x and y to show where the pointers point. Spend a few minutes to draw a box diagram of x and y to show where the pointers point.

14 LISP’s Copy-list Copy-list is a built in function but could be defined as follows (define (copy-list s) (if (not (pair? s)) (if (not (pair? s)) s (cons (copy-list (car s)) (cons (copy-list (car s)) (copy-list (cdr s))))) (copy-list (cdr s))))) There is also a copy-tree that makes a copy of the entire s-expression.

15 Append TheScheme/Lisp function append returns the concatenation of any number of lists: TheScheme/Lisp function append returns the concatenation of any number of lists: > (append ‘(a b) ‘(c d) ‘(e)) (A B C D E) Append copies all the arguments except the last. Append copies all the arguments except the last. If it didn’t copy all but the last argument, then it would have to modify the lists passed as arguments. Such side effects are very undesirable, especially in functional languages. If it didn’t copy all but the last argument, then it would have to modify the lists passed as arguments. Such side effects are very undesirable, especially in functional languages.

16 append The two argument version of append could have been defined like this. The two argument version of append could have been defined like this. (define (append2 s1 s2) (if (null? s1) s2 (cons (car s1) (append2 (cdr s1) s2)))) (append2 (cdr s1) s2)))) Notice how it ends up copying the top level list structure of it’s first argument. Notice how it ends up copying the top level list structure of it’s first argument.

17 List access functions To find the element at a given position in a list use the function list-ref (nth in CL). To find the element at a given position in a list use the function list-ref (nth in CL). > (list-ref ‘(a b c) 0) A and to find the nth cdr, use list-tail (nthcdr in CL). and to find the nth cdr, use list-tail (nthcdr in CL). > (list-tail ‘(a b c) 2) (C) Both functions are zero indexed. Both functions are zero indexed.

18 Lisp’s nth and nthcdr (define (nth (n l) (cond ((null? l) nil) (cond ((null? l) nil) ((= n 0) (car l)) ((= n 0) (car l)) (#t (nth (- n 1) (cdr l))))) (#t (nth (- n 1) (cdr l))))) (define (nthcdr n l) (cond ((null? l) nil) (cond ((null? l) nil) ((= n 0) (cdr l)) ((= n 0) (cdr l)) (#t (nthcdr (- n 1) (cdr l))))) (#t (nthcdr (- n 1) (cdr l)))))

19 Accessing lists The function last returns the last element in a list The function last returns the last element in a list  (define (last l) (if (null? (cdr l)) (if (null? (cdr l)) (car l) (car l) (last (cdr l)))) (last (cdr l)))) > (last ‘(a b c)) (C) Note: in CL, last returns the last cons cell Note: in CL, last returns the last cons cell We also have: first, second, third, and CxR, where x is a string of up to four as or ds. We also have: first, second, third, and CxR, where x is a string of up to four as or ds. E.g., cadr, caddr, cddr, cdadr, … E.g., cadr, caddr, cddr, cdadr, …

20 Member Member returns true, but instead of simply returning t, its returns the part of the list beginning with the object it was looking for. Member returns true, but instead of simply returning t, its returns the part of the list beginning with the object it was looking for. > (member ‘b ‘(a b c)) (B C) member compares objects using equal? member compares objects using equal? There are versions that use eq? and eqv? And that take an arbitrary function There are versions that use eq? and eqv? And that take an arbitrary function

21 Memf If we want to find an element satisfying an arbitrary predicate we use the function memf: If we want to find an element satisfying an arbitrary predicate we use the function memf: > (memf odd? ‘(2 3 4)) (3 4) Which could be defined like: (define (memf f l) (cond ((null? l) #f) (cond ((null? l) #f) ((f (car l)) l) ((f (car l)) l) (#t (memf f (cdr l))))) (#t (memf f (cdr l)))))

22 Dotted Lists The kind of lists that can be built by calling list are more precisely known as proper lists. The kind of lists that can be built by calling list are more precisely known as proper lists. A proper list is either the empty list, or a cons whose cdr is a proper list. However, conses are not just for building lists -- whenever you need a structure with two fields you can use a cons. However, conses are not just for building lists -- whenever you need a structure with two fields you can use a cons. You will be able to use car to refer to the first field and cdr to refer to the second. You will be able to use car to refer to the first field and cdr to refer to the second. > (set! pair (cons ‘a ‘b)) (A. B) Because this cons is not a proper list, it is displayed in dot notation. Because this cons is not a proper list, it is displayed in dot notation. In dot notation the car and cdr of each cons are shown separated by a period.

23 A pair that isn’t a proper list is called a dotted pair. A pair that isn’t a proper list is called a dotted pair. However, remember that a dotted pair isn’t really a list at all. However, remember that a dotted pair isn’t really a list at all. It is a just a two part data structure. It is a just a two part data structure. ab (A. B)

24