Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mitthögskolan 10/8/2015 1 Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.

Similar presentations


Presentation on theme: "Mitthögskolan 10/8/2015 1 Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it."— Presentation transcript:

1 Mitthögskolan 10/8/2015 1 Common Lisp LISTS

2 Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it is not the only data structure. n Common Lisp has moved on from being merely a LISt Processor.

3 Mitthögskolan 10/8/2015 3Conses n What cons really does is combines two objects into a two-part object called a cons. n Conceptually, a cons is a pair of pointers. n The first one is the car, and the second is the cdr. n Conses provide a convenient representation for pairs of any type. n The two halves of a cons can point to any kind of object, including conses. n This is the mechanism for building lists.

4 Mitthögskolan 10/8/2015 4Pairs n Lists in CL are defined as pairs. n Any non empty list can be considered as a pair of the first element and the rest of the list. n 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).

5 Mitthögskolan 10/8/2015 5 Box notation nil a A one element list (A) nil ab c A list of 3 elements (A B C)

6 Mitthögskolan 10/8/2015 6 What sort of list is this? nil ad b c > (setf z (list ‘a (list ‘b ‘c) ‘d)) (A (B C) D)

7 Mitthögskolan 10/8/2015 7 Lists of lists n Given z is (A (B C) D) n What value is returned here? –> (car (cdr z))

8 Mitthögskolan 10/8/2015 8Consp n The function consp returns true if its argument is a cons. n So listp could be defined: (defun our-listp (x) (or (null x) (consp x))) (or (null x) (consp x))) n Since everything that is not a cons is an atom, the predicate atom could be defined: (defun out-atom (x) (not (consp x))) n Remember, nil is both an atom and a list.

9 Mitthögskolan 10/8/2015 9Equality n Each time you call cons, Lisp allocates a new piece of memory with room for two pointers. n 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: > (eql (cons ‘a nil) (cons ‘a nil)) NIL

10 Mitthögskolan 10/8/2015 10Equal n We also need to be able to ask whether two lists have the same elements. n CL provides an equality predicate for this, equal. n eql returns true only if its arguments are the same object, n equal, more or less, returns true if its arguments would print the same. > (equal (cons ‘a nil) (cons ‘a nil)) T n Note: if x and y are eql, they are also equal.

11 Mitthögskolan 10/8/2015 11 Why Lisp has no pointers n One of the secrets to understanding Lisp is to realize that variables have values in the same way that lists have elements. n As conses have pointers to their elements, variables have pointers to their values.

12 Mitthögskolan 10/8/2015 12 Pointers to values n What happens, for example, when we set two variables to the same list: > (setf x ‘(a b c)) (A B C) > (setf y x) (A B C)

13 Mitthögskolan 10/8/2015 13 n The location in memory associated with the variable x does not contain the list itself, but a pointer to it. n When we assign the same value to y, Lisp copies the pointer, not the list. n Therefore, what would the value of > (eql x y) be, T or NIL?

14 Mitthögskolan 10/8/2015 14 Building Lists n The function copy-list takes a list and returns a copy of it. n The new list will have the same elements, but contained in new conses. > (setf x ‘(a b c) y (copy-list x)) y (copy-list x)) (A B C) n Spend a few minutes to draw a box diagram of x and y to show where the pointers point.

15 Mitthögskolan 10/8/2015 15Append n The Common Lisp function append returns the concatenation of any number of lists: > (append ‘(a b) ‘(c d) ‘(e)) (A B C D E) n Append copies all the arguments except the last.

16 Mitthögskolan 10/8/2015 16 List access functions n To find the element at a given position in a list we call the function nth. > (nth 0 ‘(a b c)) A n and to find the nth cdr, we call nthcdr. > (nthcdr 2 ‘(a b c)) (C) n Both nth and nthcdr are zero indexed.

17 Mitthögskolan 10/8/2015 17 Zerop and Last n The function zerop returns true if its argument is zero. > (zerop 0) T n The function last returns the last cons in a list. > (last ‘(a b c)) (C) n We also have: first, second... tenth, and n cxr, where x is a string of up to four as or ds.

18 Mitthögskolan 10/8/2015 18 Mapping functions n Common Lisp provides several mapping functions. n Mapcar is the most frequently used. n It takes a function and one or more lists, and returns the result of applying the function to elements taken from each list, until one of the list runs out:

19 Mitthögskolan 10/8/2015 19 > (mapcar #’(lambda (x) (+ x 10)) ‘(1 2 3)) ‘(1 2 3)) (11 12 13) > (mapcar #’list ‘(a b c) ‘(a b c) ‘(1 2 3 4)) ‘(1 2 3 4)) ((A 1) (B 2) (C 3))

20 Mitthögskolan 10/8/2015 20Maplist n The related function maplist takes the same arguments, but calls the function on successive cdrs of the lists: > (maplist #’(lambda (x) x) ‘(a b c)) ‘(a b c)) ((A B C) (B C) (C)) n There is also mapcan and mapc. Use the on-line Common Lisp the Language to discover what these mapping functions do.

21 Mitthögskolan 10/8/2015 21 Keyword arguments > (member ‘b ‘(a b c)) (B C) n Member returns true, but instead of simply returning t, its returns the part of the list beginning with the object it was looking for. n By default, member compares objects using eql. n You can override this behavior by employing a keyword argument.

22 Mitthögskolan 10/8/2015 22 n An example of a keyword argument is :test. n If we pass some function as the :test argument in a call to member, than that function will be used to test for equality instead of eql. > (member ‘(a) ‘((a) (z)) :test #’equal) ((A) (Z)) n Keyword arguments are always optional.

23 Mitthögskolan 10/8/2015 23 n Another keyword argument accepted by member is a :key argument. n This allows you to specify a function to be applied to each element before comparison: > (member ‘a ‘((a b) (c d)) :key #’car) ((A B) (C D))

24 Mitthögskolan 10/8/2015 24Member-if n If we want to find an element satisfying an arbitrary predicate we use the function member-if: > (member-if #’oddp ‘(2 3 5)) (3 5)

25 Mitthögskolan 10/8/2015 25adjoin n The function adjoin is like a conditional cons. n It takes an object and a list, and conses the object onto the list only if it is not already a member: > (adjoin ‘b ‘(a b c)) (A B C) > (adjoin ‘z ‘(a b c)) (Z A B C)

26 Mitthögskolan 10/8/2015 26Sets n CL has the functions, union, intersection, and set- difference for performing set operations on lists. n These functions expect exactly two lists and also the same keyword arguments as member. n Remember, there is no notion of ordering in a set. These functions won’t necessarily preserve the order of the two lists. n Give me an example of a call to union. Show the arguments and the return value.

27 Mitthögskolan 10/8/2015 27Sort n Common Lisp has a built in function called sort. n It takes a sequence and a comparison function of two arguments, and returns a sequence with the same elements, sorted according to the function: > (sort ‘(0 2 1 3 8) #’>) (8 3 2 1 0) n Sort is destructive!! n What can you do if you don’t want your list modified?

28 Mitthögskolan 10/8/2015 28 Every and Some n The functions every and some take a predicate and one or more sequences. n When given just one sequence, they test whether the elements satisfy the predicate: > (every #’oddp ‘(1 3 5)) T > (some #’evenp ‘(1 2 3)) T n If they are given more than one sequence, the predicate must take as many arguments as there are sequences, and arguments are drawn one at a time from all the sequences: > (every #’> ‘(1 3 5) ‘(0 2 4)) T

29 Mitthögskolan 10/8/2015 29 Push and Pop n The representation of lists as conses makes it natural to use them as pushdown stacks. n This is done so often that CL provides two macros for the purpose, push, and pop. n Both are defined in terms of setf. (push obj lst) is the same as (setf lst (cons obj lst) n How can pop (pop lst) be defined?

30 Mitthögskolan 10/8/2015 30 (let ((x (car lst))) (setf lst (cdr lst)) (setf lst (cdr lst)) x) x) n We also have a pushnew, which is like push but uses adjoin instead of cons. n What difference would that make?

31 Mitthögskolan 10/8/2015 31 Dotted Lists n The kind of lists that can be built by calling list are more precisely known as proper lists. n A proper list is either nil, or a cons whose cdr is a proper list. n However, conses are not just for building lists. n Whenever you need a structure with two fields you can use a cons.

32 Mitthögskolan 10/8/2015 32 n You will be able to use car to refer to the first field and cdr to refer to the second. > (setf pair (cons ‘a ‘b)) (A. B) n Because this cons is not a proper list, it is displayed in dot notation. n In dot notation the car and cdr of each cons are shown separated by a period.

33 Mitthögskolan 10/8/2015 33 n A cons that isn’t a proper list is called a dotted list. n However, remember that a dotted list isn’t really a list at all. n It is a just a two part data structure. ab (A. B)

34 Mitthögskolan 10/8/2015 34Assoc-lists n It is natural to use conses to represent mappings. n A list of conses is called an assoc-list or alist. n Such a list could represent a set of translations, for example: > (setf languages ‘((lisp. easy) (C. hard) (Pascal. good) (Ada. bad))

35 Mitthögskolan 10/8/2015 35 n Assoc-lists are slow, but convenient when engaged in rapid prototyping. n Common Lisp has a built in function, assoc, for retrieving the pair associated with a given key: > (assoc ‘C languages) (C. HARD) > (assoc ‘Smalltalk languages) NIL n Like member, assoc takes keyword arguments, including :test and :key.

36 Mitthögskolan 10/8/2015 36 Garbage Collection n Automatic memory management is one of Lisp’s most valuable features. n A Lisp system maintains a segment of memory called the heap. n The system keeps track of unused memory in the heap and allocates it to new objects. n Allocating memory from the heap is sometimes known as consing. n If such memory was never freed, Lisp would quickly run out of heap space. n So the Lisp system periodically searches through the heap, looking for memory that is no longer needed and can be reclaimed. n This is called garbage, and the scavenging operation is called garbage collection, GC.

37 Mitthögskolan 10/8/2015 37 An Example n Search is one of the basic problem solving strategies in artificial intelligence programming. n Lets look at an example of breadth-first search.

38 Mitthögskolan 10/8/2015 38Network A B C D (setf network ‘((A B C) (B C) (C D)))

39 Mitthögskolan 10/8/2015 39 sources J.E. Spragg Mitthögskolan1997


Download ppt "Mitthögskolan 10/8/2015 1 Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it."

Similar presentations


Ads by Google