CSE 341, S. Tanimoto Lisp Data Structures - 1 Data Structures in Lisp 0. Collections of associations, association lists. 1. Creating graphs with conses.

Slides:



Advertisements
Similar presentations
Introduction to LISP Programming of Pathway Tools Queries and Updates.
Advertisements

Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn.
ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
Arrays (1) You create an array in LISP by using the function (make- array ). All elements are initially set to nil. To create a 1-dimensional array of.
Common Lisp! John Paxton Montana State University Summer 2003.
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Project 1: Background Informed search : 8- puzzle world BFS, DFS and A* algorithms Heuristics : Manhattan distance, Number of misplaced tiles Lisp programming.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
ANSI Common Lisp 4. Specialized Data Structures 7 June 2003.
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.
Allegro CL Certification Program Lisp Programming Series Level I Session Top Ten Things to Know.
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.
Functional Programming Here we briefly look at what functional programming is and why we want to study it –a function, in the mathematical sense, is a.
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 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.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
06 INPUT AND OUTPUT Functional Programming. Streams Two kinds of streams  Character streams  Binary streams Character streams are Lisp objects representing.
Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.
Beyond Lists: Other Data Structures Lisp would still be a pretty decent programming language if it only contained atoms and lists –But we can go far beyond.
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.
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.
PRACTICAL COMMON LISP Peter Seibel 1.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CSE S. Tanimoto More-Concepts - 1 More Programming Language Concepts Currying Lazy Evaluation Polymorphism.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Lists CSC 358/ Outline Lab #1 / Homework #1 Lists A question List Internals of Lists List operations Extended Example.
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
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
Data Structures in Lisp
CSE (c) S. Tanimoto, 2002 AI Techniques
J.E. Spragg Mitthögskolan 1997
Imperative Data Structures
Modern Programming Languages Lecture 21 Fakhar Lodhi
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
CSE (c) S. Tanimoto, 2004 AI Techniques
Functional Programming Concepts
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Functional Programming Concepts
Lisp: Representation of Data
CSE (c) S. Tanimoto, 2002 Genetic Search
FUNCTIONS.
Defining Functions with DEFUN
Abstraction and Repetition
Data Structures in Lisp
LISP: Basic Functionality
Data Structures in Lisp
Functional Programming Concepts
LISP: Basic Functionality
Bindings, Scope, and Extent
Common Lisp II.
LISP: Basic Functionality
CSE (c) S. Tanimoto, 2004 Genetic Search
Lisp: Representation of Data
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Presentation transcript:

CSE 341, S. Tanimoto Lisp Data Structures - 1 Data Structures in Lisp 0. Collections of associations, association lists. 1. Creating graphs with conses. RPLACA and RPLACD (destructive editing) 2. Adjacency lists. 3. Sequences 4. Arrays 5. Hashtables

CSE 341, S. Tanimoto Lisp Data Structures - 2 Collections of Associations A collection of associations can be thought of as a set, a function, or a many-many mapping. CONS is the fundamental mechanism for association in Lisp. An association list is a list of CONSes. However, for reasons of efficiency, there are alternative to represent collections of associations in Lisp.

CSE 341, S. Tanimoto Lisp Data Structures - 3 Direct Manipulation of Conses > (setq x (cons 'a 'b)) (A. B) > (rplaca x 'c) (C. B) > x (C. B) > (rplacd x 'd) (C. D) > (rplacd x x) (C C C C C C C C C C... ) >

CSE 341, S. Tanimoto Lisp Data Structures - 4 Adjacency Lists Nowadays, RPLACA and RPLACD are less used. Straight lists are more common. Memory space is less of a concern. (setq nodes '(a b c d)) (setq arc-lists '( (a (b c)) (b (d)) (c (d)) (d ()) ) (defun is-edge (x y) (member y (second (assoc x arc-lists))) ) (is-edge 'a 'c) ; => (C) i.e., true. (is-edge 'a 'd) ; => NIL A C B D

CSE 341, S. Tanimoto Lisp Data Structures - 5 Sequences A sequence can be thought of as a mapping from {0, 1,..., n-1} to a set of range elements. A string is a sequence of characters. A list is a particular form of sequence of lisp objects. (typep "abc" 'sequence) ; => T. (typep '(a b c) 'sequence) ; => T. (concatenate 'string "abc" "def"); => "abcdef" (concatenate 'list '(a b) '(c d)); =>(a b c d) (elt '(a b c d) 2) ;=> C

CSE 341, S. Tanimoto Lisp Data Structures - 6 Arrays An array is a mapping from a set of index tuples to a set of range elements. (setq a (make-array '(5 10):initial-element 1)) #2A(( ) ( ) ( )) (setf (aref a 4 9) 2) 2 (let ((sum 0)) (dotimes (i 5 sum)(dotimes (j 10) (incf sum (aref a i j)) )) ) 51

CSE 341, S. Tanimoto Lisp Data Structures - 7 Hashtables Association lists are linear and slow. For larger sets of associations, hashtables tend to be much faster. > (let ((h (make-hash-table))) (defun get-h (key) (gethash key h)) (defun put-h (key value) (setf (gethash key h) value)) ) PUT-H > (put-h 'x 'y) Y > (get-h 'x) Y ; The value associated with X T ; True, there is a value. >

CSE 341, S. Tanimoto Lisp Data Structures - 8 MAPHASH ; Changes each entry in THE-HASH-TABLE, ; replacing each positive value with its ; square root, and removing negative values. (maphash ; Steele’84, p285. #'(lambda (key value) (if (minusp value) (remhash key the-hash-table) (setf (gethash key the-hash-table) (sqrt value)))) the-hash-table) ; returns NIL