J.E. Spragg Mitthögskolan 1997

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
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.
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.
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
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.
1 LISP III. 2 Functional programming l Definition From the "comp.lang.functional FAQ" Functional programming is a style of programming that emphasizes.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
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.
CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt1 CSCI 2210: Programming in Lisp Programming Techniques Data Structures More Built-in.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
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.
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.
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
Yu-Tzu Lin ( 林育慈 )
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
Functional Programming 02 Lists
PRACTICAL COMMON LISP Peter Seibel 1.
PRACTICAL COMMON LISP Peter Seibel 1.
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 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.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
11 Speed & Debug.  Lisp is really two languages:  A language for writing fast programs  A language for writing programs fast  In the early stage,
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Control in LISP More on Predicates & Conditionals.
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)
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Lists CSC 358/ Outline Lab #1 / Homework #1 Lists A question List Internals of Lists List operations Extended Example.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Section 15.4, 15.6 plus other materials
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
Lisp S-Expressions: ATOMs
Example of formula (defun roots (a b c) (list
Containers and Lists CIS 40 – Introduction to Programming in Python
Lists in Lisp and Scheme
Arrays in C.
LISP A brief overview.
First Lecture on Introductory Lisp
Modern Programming Languages Lecture 21 Fakhar Lodhi
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Lisp: Representation of Data
Peter Seibel Practical Common Lisp Peter Seibel
Today’s topics Abstractions Procedural Data
Data Structures & Algorithms
LISP: Basic Functionality
LISP: Basic Functionality
Common Lisp II.
LISP: Basic Functionality
Lisp.
Lisp: Representation of Data
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
Lists in Lisp and Scheme
Presentation transcript:

J.E. Spragg Mitthögskolan 1997 Common Lisp LISTS J.E. Spragg Mitthögskolan 1997

Lists Lists are one of the fundamental data structures in Lisp. However, it is not the only data structure. Common Lisp has moved on from being merely a LISt Processor.

Conses What cons really does is combines two objects into a two-part object called a cons. Conceptually, a cons is a pair of pointers. The first one is the car, and the second is the cdr. 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. This is the mechanism for building lists.

Pairs Lists in CL are defined as pairs. 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).

Box notation nil a A one element list (A) nil c a b A list of 3 elements (A B C)

What sort of list is this? d nil c b > (setf z (list ‘a (list ‘b ‘c) ‘d)) (A (B C) D)

Lists of lists Given z is (A (B C) D) What value is returned here? > (car (cdr z))

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

Equality Each time you call cons, Lisp allocates a new piece of memory with room for two 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: > (eql (cons ‘a nil) (cons ‘a nil)) NIL

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

Why Lisp has no pointers 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.

Pointers to values What happens, for example, when we set two variables to the same list: > (setf x ‘(a b c)) (A B C) > (setf y x)

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, Lisp copies the pointer, not the list. Therefore, what would the value of > (eql x y) be, T or NIL?

Building Lists 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. > (setf x ‘(a b c) 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.

Append The Common 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.

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

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

Mapping functions Common Lisp provides several mapping functions. Mapcar is the most frequently used. 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:

> (mapcar #’(lambda (x) (+ x 10)) ‘(1 2 3)) (11 12 13) ‘(1 2 3)) (11 12 13) > (mapcar #’list ‘(a b c) ‘(1 2 3 4) ) ((A 1) (B 2) (C 3)) Takes one list Takes two lists

Maplist 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) (B C) (C)) There is also mapcan and mapc. Use the on-line Common Lisp the Language to discover what these mapping functions do.

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

Using the keyword argument An example of a keyword argument is :test. 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)) Keyword arguments are always optional. Tells to use this function to do equality comparison

Using the key argument Another keyword argument accepted by member is a :key argument. 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))

Member-if If we want to find an element satisfying an arbitrary predicate we use the function member-if: > (member-if #’oddp ‘(2 4 6 3 7 14)) (3 7 14) First odd number from the left

adjoin The function adjoin is like a conditional cons. 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) b already existed Z did not already existed

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

Sort Common Lisp has a built in function called sort. 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) Sort is destructive!! What can you do if you don’t want your list modified?

Every and Some Every element was odd Some element was even The functions every and some take a predicate and one or more sequences. When given just one sequence, they test whether the elements satisfy the predicate: > (every #’oddp ‘(1 3 5)) T > (some #’evenp ‘(1 2 3)) 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)) Every element was odd Some element was even Because 1>0 and 3>2 and 5 > 4

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

How can pop (pop lst) be defined? this is how pop is defined (let ((x (car lst))) (setf lst (cdr lst)) x) We also have a pushnew, which is like push but uses adjoin instead of cons. What difference would that make? Keeps the stack as a set

Dotted Lists The kind of lists that can be built by calling list are more precisely known as proper lists. A proper list is either nil, 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.

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) 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.

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

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

More details about association lists Assoc-lists are slow, but convenient when engaged in rapid prototyping. 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 Like member, assoc takes keyword arguments, including :test and :key.

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

An Example Search is one of the basic problem solving strategies in artificial intelligence programming. Lets look at an example of breadth-first search. Discuss graphs, labeled graphs, oriented graphs, semantic networks and search on these data structures

Network B A D C (setf network ‘((A B C) (B C) (C D)))