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

Slides:



Advertisements
Similar presentations
Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn.
Advertisements

09 Examples Functional Programming. Tower of Hanoi AB C.
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
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.
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)
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
1 LISP III. 2 Functional programming l Definition From the "comp.lang.functional FAQ" Functional programming is a style of programming that emphasizes.
Commands and predicates LISP functions are divided into 2 classes. Predicates are functions that return boolean values i.e. t or nil. The rest are commands.
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.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
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.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
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.
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.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
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.
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
Functional Programming
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.
Common lisp A functional programming language. Useful URL:
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
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.
Variables, Values, Atoms, Lists Variables in Lisp differ from other languages –They are not declared by type their type is determined at run-time, and.
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.
Functional Programming: Lisp MacLennan Chapter 10.
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
Section 15.4, 15.6 plus other materials
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
Lisp S-Expressions: ATOMs
Modern Programming Languages Lecture 20 Fakhar Lodhi
Lists in Lisp and Scheme
LISP A brief overview.
First Lecture on Introductory Lisp
J.E. Spragg Mitthögskolan 1997
Imperative Data Structures
Modern Programming Languages Lecture 21 Fakhar Lodhi
LISP A brief overview.
Lisp: Representation of Data
Functional Programming: Lisp
Data Structures in Lisp
LISP: Basic Functionality
Data Structures in Lisp
LISP: Basic Functionality
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.
LISP primitives on sequences
Lists in Lisp and Scheme
Presentation transcript:

ANSI Common Lisp 3. Lists 20 June 2003

Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists

Conses (cons, car and cdr) Cons Combine two objects into a two-part pair. The first part is car and the second is cdr. >(setf x (cons a b)) (A. B) >(car x) A >(cdr x) B Box notation

Conses (lists) Lists can be represented as conses. >(cons a nil) (A) >(cons 'a (cons 'b (cons 'c nil))) (A B C) A list can have any kind of object as elements, including another list (nested list). > (setf x (list a (list b c) d) (A (B C) D)

List Functions list > (list a b c) (A B C) car and cdr > (car (a b c)) A > (cdr (a b c)) (B C) listp (returns T if the argument is either a cons or a NIL) >(listp (a b c)) T

List Functions consp (returns T for any cons) >(consp (a b c)) T atom (returns T for any none-cons) >(atom NIL) T append (concatenates any number of lists) >(append '(a (b c)) '(d e)) (A (B C) D E)

List Functions -- Equality eq (returns T iff object1 and object2 are in the same memory location.) >(eq x x) T >(eq (cons 1 2) (cons 1 2)) NIL eql (returns T iff object1 and object2 are eq or the same character/number.) >(eql x x) T >(eql (cons 1 2) (cons 1 2)) NIL

List Functions -- Equality equal (returns T if its arguments prints same.) >(equal (cons 1 2) (cons 1 2)) T > (equal '(a b c) '(a (b) c)) NIL equalp (returns T iff object1 and object2 are equal, char-equal, or=, or are cons whose cars and cdrs are equalp; or are hash tables with same test function and number of entries whose keys are all associated with equalp values.)

List Functions copy-list The new list has the same elements as the original, but not eql to. >(setf x (a b c) y (copy-list x)) (A B C)

List Functions Difference between copy-list and setf >(setf x (a b c)) (A B C) >(setf y x) (A B C) >(eql x y) T >(setf (car y) 'aa) >x (AA B C)

List Functions -- Access first, second, third, fourth, …, tenth >(third (a b c d)) C cxxxxr (cadr, caddr, cadddr…) >(caddr '(a b c d e f)) C nth >(nth 0 (a b c)) A nthcdr >(nthcdr 2 (a b c)) (C)

List Functions – mapping functions mapcar Takes a function and one or more lists, and returns the result of applying the function to elements taken from each list, until some list runs out. >(mapcar #list ( ) (a b c)) ((1 A) (2 B) (3 C)) maplist Takes the same arguments as mapcar, but applies the function on successive cdrs of the lists. >(maplist #(lambda (x) x) (a b c)) ((A B C) (B C) (C))

List Functions length >(length (a b c d)) 4 subseq The 2 nd argument (required) is the position of the 1 st element to be included. The 3 rd argument (optional) is the position of the first element not to be included. >(subseq (a b c d) 1 3) (B C) >(subseq (a b c d) 1) (B C D)

List Functions reverse >(reverse (a (b c) d)) (D (B C) A) sort Takes a sequence and a comparison function of two arguments, and returns a sequence with the same elements sorted according to function. The sort function is destructive. The sequence given to sort may be modified. >(sort ( ) #>) ( )

List Functions every and some Take a predicate and one or more sequences. If given only one sequence, they test whether the elements satisfy the predicate. If given more then one sequences, they test whether the elements, drawn one at a time from all sequences, satisfy the predicate. >(every #oddp (1 3 5)) T (some #evenp (1 2 3)) T >(every #< (1 2 3) (4 5 6)) T

Trees The conses (lists) can be considered as binary trees, where the car represents the left sub-tree and the cdr represents the right sub-tree.

Trees General Operation Forms on Trees Recursing down both the car and cdr. >(copy-tree (a (b c) d)) (A (B C) D) (defun copy-tree (tr) (if (atom tr) tr (cons (copy-tree (car tr)) (copy-tree (cdr tr)))))

Trees Substitution >(setf x '(and (integerp x) (zerop (mod x 2)))) (AND (INTEGERP X) (ZEROP (MOD X 2))) >(substitute 'y 'x x) (AND (INTEGERP X) (ZEROP (MOD X 2))) >(subst 'y 'x x) (AND (INTEGERP Y) (ZEROP (MOD Y 2)))

Sets Lists can be considered as sets. Each element of a list is a member of the set it represents. adjoin >(adjoin b (a b c)) (A B C) >(adjoin d (a b c)) (D A B C) union >(union (a b c) (c b s)) (A C B S)

Sets intersection >(intersection (a b c) (b b c)) (B C) set-difference >(set-difference (a b c d e) (b e)) (A D C) The union, intersection and set-difference functions dont guarantee the original order of elements will be preserved.

Sets -- member member function Tests whether an object is a member of a set. If yes, it returns the part of list beginning with the object its looking for. By default, it compares objects using eql. The :test keyword lets you change the comparison function. >(member (a) ((b) (a) (z))) NIL >(member (a) ((b) (a) (z)) :test #equal) ((A) (Z))

Sets -- member member function The :key keyword is used to specify a function to be applied to each element of the list before comparison. >(member a ((a b) (c d)) :key #car) ((A B) (C D)) If theres an element whose car is a. member-if Find an element satisfying some predicate. >(member-if #oddp (2 3 4)) (3 4)

Stacks Lists can be considered as stacks (FILO). push and pop (push x y) pushes x onto the front of list y; pop(y) removes and returns the 1 st element of list y. >(setf x (a)) >(push b x) (B A) >(pop x) B

Stacks pushnew Takes the same arguments as push(x, y). If object x already exists in list y, then x wont be pushed in anymore. >(setf x (a)) >(push b x) (B A) >(pushnew b x) (B A)

Dotted Lists Proper lists Either a NIL or a cons whose cdr is a proper list. Dotted Lists >(setf pair (cons a b)) (A. B) Lists can be represented as dotted lists. >(a. (b. (c. nil))) (A B C)

Assoc-lists Assoc-list is a list of conses. It can be used as mapping or hash table. >(setf trans ((+. add) (-. subtract))) ((+. add) (-. subtract)) >(assoc + trans) (+. add) The assoc function can take :key and :test keywords.