Imperative Data Structures

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.
1 Programming Languages and Paradigms Lisp Programming.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
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.
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
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.
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)
ANSI Common Lisp 4. Specialized Data Structures 7 June 2003.
CSE 341, S. Tanimoto Lisp Data Structures - 1 Data Structures in Lisp 0. Collections of associations, association lists. 1. Creating graphs with conses.
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
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
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.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
Multiple Returns and Extra Parameters “LISP is unique in its capability to return more than one value (or no value at all) from a form. This neatly avoids.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
06 INPUT AND OUTPUT Functional Programming. Streams Two kinds of streams  Character streams  Binary streams Character streams are Lisp objects representing.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
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.
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
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.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
PRACTICAL COMMON LISP Peter Seibel 1.
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.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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 CSE 2337 Chapter 7 Organizing Data. 2 Overview Import unstructured data Concatenation Parse Create Excel Lists.
PRACTICAL COMMON LISP Peter Seibel 1.
Macros CSC 358/ Outline  Homework #6  Macro Intro  Backquote  Macros  Modify macros  Read Macros.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
Sets and Maps Chapter 9.
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
Lisp S-Expressions: ATOMs
Hashing & HashMaps CS-2851 Dr. Mark L. Hornick.
Example of formula (defun roots (a b c) (list
Data Structures in Lisp
Arrays An array in PHP is an ordered map
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Hash table another data structure for implementing a map or a set
Advanced Associative Structures
CS313D: Advanced Programming Language
J.E. Spragg Mitthögskolan 1997
CIS16 Application Development and Programming using Visual Basic.net
Data abstraction, revisited
Allegro CL Certification Program
Scheme: Basic Functionality
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Microsoft Visual Basic 2005: Reloaded Second Edition
Lisp: Representation of Data
Sets and Maps Chapter 9.
Peter Seibel Practical Common Lisp Peter Seibel
DATA STRUCTURE.
Data Structures in Lisp
LISP: Basic Functionality
Data Structures in Lisp
LISP: Basic Functionality
Common Lisp II.
Indexing, Access and Database System Architecture
LISP: Basic Functionality
Lisp.
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:

Imperative Data Structures “Get your data structures correct first, and the rest of the program will write itself.” -Davids Johnson, Assen, The Netherlands

Why Does LISP Need Them? Lists can represent any combination of data you could need Strings and arrays are just lists of data Association lists are like hash tables because you can look up values by keys But they are inefficient Can only access at the front The simplest access operations are linear time on average

Strings We already used strings in our IO operations, and we know how to return them using format >(format nil "Blah Blah ~A" "Blah") "Blah Blah Blah" Strings are one instance of a sequence: Lists, strings, arrays and vectors are sequences But strings are still atomic (atom "String") → T

String Functions (char s i) returns the ith character of string s (zero-based index) With setf, can be used to destructively modify character in a string (string c) takes a character c and returns a string containing the single character represented by c

Make-String You can create strings of a specified size using make-string >(make-string 10) " " You can specify an initial element using the appropriate keyword Default is #\Space (depends on implementation) >(make-string 10 :initial-element #\h) "hhhhhhhhhh"

Intern (intern s) returns the corresponding symbol for a given string s If the symbol already exists, a second value of :INTERNAL is returned Otherwise a second value of NIL is returned >(intern "A") A NIL :INTERNAL >(intern "a") |a|

String-Trim Removes leading and trailing characters from a string First argument is characters to remove, second is string to remove from >(string-trim "24" "2 3 3 4") " 3 3 " >(string-trim "24 " "2 3 3 4") "3 3" >(string-trim "24 3" "2 35353 4") "535" string-left-trim and string-right-trim also exist

Arrays A single dimensional array is a vector Strings are vectors of characters Create arrays using make-array (make-array n) makes a vector of size n (make-array '(n1 n2 n3 …)) makes a multi-dimensional array with sizes n1, n2, n3, etc. along the corresponding dimensions :initial-element allows specification of a uniform initial value (default nil;depends on implementation) :initial-contents allows you to fully specify the initial contents as a list

Make-Array Examples >(make-array 9) #(NIL NIL NIL NIL NIL NIL NIL NIL NIL) >(make-array 9 :initial-element 5) #(5 5 5 5 5 5 5 5 5) >(make-array '(3 2)) #2A((NIL NIL) (NIL NIL) (NIL NIL)) >(make-array 3 :initial-contents '(1 3 5)) #(1 3 5) >(make-array '(2 2) :initial-contents '((1 3) (2 4))) #2A((1 3) (2 4)) >(make-array '(2 2) :initial-contents '(1 2 3)) Error

Accessing Array Elements aref is used to access array elements An index is needed for each dimension of the array >(setf x (make-array '(3 2) :initial-contents '((1 2) (3 4) (5 6)))) #2A((1 2) (3 4) (5 6)) >(aref x 0 0) 1 >(aref x 3 1) Error >(aref x 2 1) 6

Vectors One-dimensional arrays Can create and fill using vector >(vector "a" 3 'foo) #("a" 3 FOO) Can access vectors using aref, but can also use the faster svref for vectors

Position Search for an element in any sequence Keyword arguments include :key, :test, :from-end, :start and :end >(position #\o "location") 1 >(position #\o "location" :start 2) 6 >(position #\o "location" :start 2 :end 4) NIL >(position #\o "location" :from-end t) >(position 'a '((c d) (a b)) :key #'car) >(position '(a b) '((c d) (a b)) :test #'equal)

Other Sequence Functions We’ve already seen subseq, remove and remove-if Others include position-if, find, find-if reduce folds a sequence into a single value (reduce #'fn '(a b c d)) is the same as (fn (fn (fn 'a 'b) 'c) 'd) >(reduce #'cons '(a b c d e)) ((((A . B) . C) . D) . E)

Hash Tables More efficient than association lists Create using make-hash-table Access using get-hash Add entries using setf on the get-hash Remove elements using remhash

Make-Hash-Table >(make-hash-table) #<hash-table 083cae8c> Can set initial size using :size Can change the test used for looking up entries using :test Default is eql

Hash Table Examples >(setf ht (make-hash-table)) #<hash-table 083caa2c> >(gethash 'color ht) NIL >(setf (gethash 'color ht) 'red) RED T >(remhash 'color ht)

Maphash Maphash takes a function of two arguments and a hash table and calls that function on every key/value pair in the table, in no particular order >(setf (gethash 'shape ht) 'round) ROUND >(setf (gethash 'size ht) 30) 30 >(maphash #'(lambda (k v) (format t "~A = ~A~%" k v)) ht) SIZE = 30 SHAPE = ROUND NIL