CSCI 2210 - Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt1 CSCI 2210: Programming in Lisp Programming Techniques Data Structures More Built-in.

Slides:



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

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)
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
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.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
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.
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)
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
ANSI Common Lisp 4. Specialized Data Structures 7 June 2003.
Mapping And Iteration So far, the only Lisp mechanism we have considered, which allows an action to be performed repeatedly is recursion. Most programming.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Common Lisp Object System (CLOS)
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
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.
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.
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.
For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.
Functional Programming 02 Lists
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Data Abstraction and Mapping.
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.
Common Lisp! John Paxton Montana State University Summer 2003.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CSCI 2210: Programming in Lisp
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.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
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.
PRACTICAL COMMON LISP Peter Seibel 1.
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.
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.
Basic Introduction to Lisp
Search as a problem solving technique. Consider an AI program that is capable of formulating a desired goal based on the analysis of the current world.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
Section 15.4, 15.6 plus other materials
Lists in Lisp and Scheme
J.E. Spragg Mitthögskolan 1997
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
Lisp: Using Functions as Data
Announcements Quiz 5 HW6 due October 23
Peter Seibel Practical Common Lisp Peter Seibel
Abstraction and Repetition
Today’s topics Abstractions Procedural Data
Lisp: Using Functions as Data
Common Lisp II.
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
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Presentation transcript:

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt1 CSCI 2210: Programming in Lisp Programming Techniques Data Structures More Built-in Functions

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt2 Functional Programming  Functional Programming  Writing programs that return values –Instead of modifying things (side effects) –Avoid things like setf  Dominant paradigm in Lisp  Allows interactive testing –Can immediately test anything at any time –Don't have to worry that it will mess up some "state" –Don't have long edit-compile-run cycle –Compare this to testing in other languages

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt3 Functions as Objects  Functions are regular objects  Function  Returns the object associated with the function > (function +) #  Can use #' macro as a convenience > #'+ #  Example > (funcall #' ) > (apply #'+ '(1 2 3)) ; Note: These are equivalent to ( )

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt4 Funcall  Funcall –Template (funcall #' … ) Calls the specified procedure with the given argments –Equivalent to ( … ) –Example > (funcall #' ) ;; Same as ( ) 12 –Example 2 > (defun eval-infix (arg1 operator arg2) (funcall operator arg1 arg2)) > (eval-infix 3 #'+ 2)  Useful if you need to pass in a procedure name, to be applied at a future time

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt5 Apply  Apply  Similar to Funcall  (typically) takes two arguments, –procedure object –list of arguments to be passed to procedure object (funcall #' ) ;; Same as ( ) (apply #'+ '(3 2 7)) ;; Same as ( )  Apply can actually take additional arguments –Extra arguments are combined into a single list –Following are equivalent (apply #'+ '( )) (apply #' '(4 5 6)) Argument fed to + is (append (list 1 2 3) ‘(4 5 6))

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt6 Data Representation Using Lists  Record information about courses  Basic definition > (setf lisp-course-info '(CSCI ; course number (Programming in Lisp); course name (Alok Mehta); instructor (Computer Science))); department (CSCI (PROGRAMMING IN LISP) … )  What is the name of the course ‘lisp-course-info’? > (second lisp-course-info) (PROGRAMMING IN LISP)  Poor practice - Depends on the ordering of the list  What if the order changes, or you need to add something

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt7 Modified Course Structure  Example, if we need to add credit hours information (setf lisp-course-info '(CSCI ; Credit hours (Programming in Lisp); Course name (Alok Mehta); Instructor (Computer Science))); Department  All programs that access name of course-1 have to change from using SECOND to using THIRD > (third lisp-course-info)

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt8 Association Lists  Better way to represent data: Association Lists (setf lisp-course-info '( (course-number CSCI221001) (credit-hours 1) (name (Programming in Lisp)) (instructor (Alok Mehta)) (department (Computer Science))))  What is the name of ‘lisp-course-info’? > (second (third lisp-course-info)) (PROGRAMMING IN LISP) > (assoc 'name lisp-course-info) (NAME (PROGRAMMING IN LISP)) > (second (assoc 'name lisp-course-info)) (PROGRAMMING IN LISP)

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt9 Simple Database  A database as a list of “records” (setf course-database '( ((course-number CSCI221001) (credit-hours 1) (name (Programming in Lisp)) (instructor (Alok Mehta)) (department (Computer Science))) ((course-number CSCI220001) (credit-hours 1) (name (Programming in C++))) ))  What is the name of the lisp course? (second (assoc 'name (first course-database)))

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt10 Access Functions  Write functions to access low-level details (defun get-course-name (course) (second (assoc 'name course))) –Don’t have to remember how data is stored –Can change storage details easily –Access to data is achieved at a higher level –Easier to read and understand –More maintainable code  Write functions to construct and manipulate data (defun make-course (&key course-number credit-hours name instructor department) (list (list 'course-number course-number) (list 'credit-hours credit-hours) (list 'name name) (list 'instructor instructor) (list 'department department) ))

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt11 Calling Access Functions > (setf course-1 (make-course :name '(Programming in Java) :instructor '(Alok Mehta))) ((COURSE-NUMBER NIL) (CREDIT-HOURS NIL) (NAME (PROGRAMMING IN JAVA)) (INSTRUCTOR (ALOK MEHTA)) (DEPARTMENT NIL)) > (get-course-name course-1) (PROGRAMMING IN JAVA)

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt12 A simple database example (setf courses (list (make-course :course-number 'CSCI :name '(Programming in C++) :instructor '(Louis Ziantz)) (make-course :course-number 'CSCI :name '(Programming in Lisp) :instructor '(Alok Mehta)) (make-course :course-number 'CSCI :name '(Programming in Java) :instructor '(Alok Mehta)) (make-course :course-number 'CSCI :name '(Programming in Perl) :instructor '(Louis Ziantz)) ))

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt13 Which courses are offered?  Example Usage > (get-course-numbers courses) (CSCI CSCI CSCI CSCI223001) > (defun get-course-number (course) (second (assoc 'course-number course))) > (defun get-course-numbers (course-list) (if (endp course-list) NIL (cons (get-course-number (first course-list)) (get-course-numbers (rest course-list)))))  Implements a “Transformation” SQL Equivalent: SELECT course_number FROM courses;

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt14 Which are taught by Alok?  List the courses taught by Alok Mehta > (get-courses-taught-by-alok courses) (((COURSE-NUMBER CSCI221001) …) ((COURSE-NUMBER CSCI222001)...)...) (defun get-course-instructor (course) (second (assoc 'instructor course))) (defun taught-by-alok-p (course) (if (equal '(Alok Mehta) (get-course-instructor course)) t NIL)) (defun get-courses-taught-by-alok (course-list) (cond ((endp course-list) NIL) ((taught-by-alok-p (first course-list)) (cons (first course-list) (get-courses-taught-by-alok (rest course-list)))) (t (get-courses-taught-by-alok (rest course-list)))))

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt15 How many are taught by Alok  List the courses taught by Alok Mehta [Filter] > (get-courses-taught-by-alok courses)  Implements “Filtering” SQL: SELECT * FROM COURSES WHERE instructor = ‘Alok Mehta’;  How many courses are taught by Alok? [Count] > (length (get-courses-taught-by-alok courses)) 2 SQL: SELECT COUNT(*) FROM COURSES WHERE instructor = ‘Alok Mehta’;  What is the first course taught by Alok? [Find] > (first (get-courses-taught-by-alok courses)) ((COURSE-NUMBER CSCI221001) (CREDIT-HOURS NIL) (NAME (PROGRAMMING IN LISP)) (INSTRUCTOR (ALOK MEHTA)) (DEPARTMENT NIL))

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt16 More efficient way to Count  A more efficient way to count  Don’t have to get all courses first! (defun count-courses-taught-by-alok (course-list) (cond ((endp course-list) 0) ((taught-by-alok-p (first course-list)) (+ 1 (count-courses-taught-by-alok (rest course-list)))) (t (count-courses-taught-by-alok (rest course-list)))))  A more efficient way to find first course (defun find-first-course-taught-by-alok (course-list) (cond ((endp course-list) nil) ((taught-by-alok-p (first course-list)) (first course-list)) (t (find-first-course-taught-by-alok (rest course-list)))))

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt17 Cliches  Many procedures have a common template (defun (input-list) (if (endp input-list) nil (cons ( (first input-list)) ( (rest input-list)))))  Example: –List-transformer: get-course-numbers –Element-transformer: get-course-number (defun get-course-numbers (course-list) (if (endp course-list) NIL (cons (get-course-number (first course-list)) (get-course-numbers (rest course-list)))))  This template is frequently used  Lisp uses the primitive (MAPCAR) to make this easier

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt18 Mapcar and Maplist  Mapcar - MAPs a function to successive CARs  Examples > (mapcar #'(lambda (x) (* x 2)) '( )) ( ) > (mapcar #'= '(1 2 3) '(3 2 1)) (NIL T NIL)  Other MAPxxxx functions  Maplist  Mapc  Mapcan

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt19 Mapcar  MAPCAR (mapcar ) Applies to each element of  Example > (mapcar #'oddp '(1 2 3)) (T NIL T) > (mapcar #'get-course-number courses) (CSCI CSCI CSCI CSCI223001) > (mapcar #'= '(1 2 3) '(3 2 1)) (NIL T NIL)

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt20 Remove-If, Remove-If-Not  Remove-If, Remove-If-Not > (remove-if-not #'taught-by-alok-p courses) [Removes all courses for which “taught-by-alok-p” returns NIL] This will return all courses taught by Alok > (remove-if #'taught-by-alok-p courses) [Removes all courses for which “taught-by-alok-p” returns non-NIL] This will return all courses NOT taught by Alok  Count-If, Count-If-Not > (count-if #'taught-by-alok-p courses) Counts the number of elements for which “taught-by-alok-p” returns non-NIL  Find-If, Find-If-Not > (find-if #'taught-by-alok-p courses) Returns the FIRST element of courses for which “taught-by-alok-p” returns non- NIL

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt21 Lambda  Lambda generates an unnamed function  Example > ((lambda (x) (+ x 100)) 1) 101 –Defines an unnamed function which takes one parameter (x), and has the body (+ x 100) –The function adds 100 to its argument –Invokes the unnamed function with a value of 1 for the paramter –The return value is = 101  Why use unnamed functions?  Analogy to Java's "anonymous" classes –Don’t have to make up names for procedures –Definition of procedure is close to where it is used

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt22 Lambda Procedures  Useful if you need an “anonymous” procedure  Don’t want to give it a name –A named procedure (defun taught-by-alok-p (course) (if (equal '(Alok Mehta) (get-course-instructor course)) t NIL)) –An equivalent un-named procedure (lambda (course) (if (equal '(Alok Mehta) (get-course-instructor course)) t NIL)) –Example usage: How many courses are taught by Alok? (count-if #'(lambda (course) (if (equal '(Alok Mehta) (get-course-instructor course)) t NIL)) courses)

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt23 Lambda procedures  Advantage  Don’t have to make up names for procedures  Definition of procedure is close to where it is used (defun get-courses-taught-by-alok (course-list) (remove-if-not #'(lambda (x) (equal '(Alok Mehta) (get-course-instructor x))) course-list))

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt24 Data Structures Using Lists  There is no explicit pointer manipulation in Lisp  In Lists, handled automatically via cons cells  Variables are pointers to values –pointer to a cons cell; or –pointer to an atomic value  Lists  Powerful for implementing variety of data structures  Examples of data structures are on next few slides

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt25 Linked List DS using Lists  Linked List  Copy-List  Built-in function that takes a list and returns a copy of it > (defun our-copy-list (lst) (if (atom lst) lst (cons (car lst) (our-copy-list (cdr lst))))) ABC X

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt26 Stack DS using Lists  Push, Pop macros implement the stack data structure using Lists ABC X

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt27 Binary Tree DS Using Lists  Cons cell of List can be used as the left/right branches of a binary tree (a (b c) d) A B C D

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt28 N-ary Tree DS Using Lists

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt29 Tree Functions  Copy-tree  Built-in function to copy a tree  Similar to Copy-List, except it does a deep recursive copy on the CAR as well as the CDR  Doubly recursive  Example implementation > (defun our-copy-tree (tr) (if (atom tr) tr (cons (our-copy-tree (car tr)) (our-copy-tree (cdr tr)))))  Other built-in tree functions  subst, tree-equal

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt30 Set DS Using Lists  Member > (member 'b '(a b c)) (B C) > (member '(a) '((a) (z)) :test #'equal) ((A) (Z)) > (member 'a '((a b) (c d)) :key #'car) ((A B) (C D))  Adjoin - Adds an element to a set [conditional cons] > (adjoin 'b '(a b c)) (A B C) > (adjoin 'z '(a b c)) (Z A B C)  Union, Intersection, Set-Difference > (union '(a b c) '(c b s)) (A C B S) > (intersection '(a b c) (b b c)) (B C) > (set-difference '(a b c d e) '(b e)) (A C D)

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt31 Sequences  Sequence - an ordered series of objects > (length '(a b c)) 3 > (subseq '(a b c d e f) 2 5) ; Index of A=0; B=1;... (C D E) ; Note: Returns elements at index X, where 2<=X<5 > (reverse '(a b c)) (C B A) > (sort '( ) #'<) ( ) > (sort '(A B C D R S C A E) #'string<) (A A B C C D E R S) > (every #'> '(1 3 5) '(0 2 4)) ;((1>0)&&(3>2)&&(5>4)) T > (some #'= '(1 3 5) '(0 3 5)) ;((1=0)||(3=3)||(5=5)) T

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt32 Dotted Lists  A proper list is either NIL or a CONS who's CDR is a proper list  Dotted List  Really a two-part structure  The "CDR" part doesn't point to a list > (setf x (cons 'a 'b)) (A. B) > (setf x '(a. b)) ; Shorthand (A. B)  Don't use dotted lists  Not really needed A X B

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt33 Graph DS Using Lists  A Graph consists of vertices and edges  One way to represent a graph ((v1. Neighbors-of-v1) (v2. Neighbors-of-v2) …)  Example > (setf graph-1 '((a b c) (b c) (c d))) ((A B C) (B C) (C D)) A B C D

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt34 Specialized Data Structures  We've seen that Lists are extremely versatile  I said that everything in Lisp is an Atom or a List  I lied...  I sort of lied…  depends on what the meaning of Atom is…  There are many types of atoms  More efficient than Lists

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt35 Vector  Vector - one dimensional array > (setf x (vector "a" 'b 3)) #("a" B 3) ; The # is a short-hand/notation for array  Vectors are atoms, not lists > (atom x) ; A vector is an Atom T > (listp x) ; A vector is not a List NIL > (length x) ; Length works for lists and vectors 3 > (null (vector)) ; An empty vector #() is not NIL NIL  AREF is used to update/access elements > (aref x 0) ; get the zero'th element of x "a" > (setf (aref x 1) 'c) C > x #("a" C 3)

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt36 Example: Binary Search (defun bin-search (obj vec &optional (start 0) (end (- (length vec) 1)) &aux (range (- end start)) (midpt (+ start (round (/ range 2))))) (print (list obj vec start end)) ; Optional, to display (if (> start end) NIL ; We crossed over (let ((obj_at_midpt (aref vec midpt))) (cond ((equal obj obj_at_midpt) obj) ; Found ((< obj obj_at_midpt) ; In lower half (bin-search obj vec start (- midpt 1))) ((> obj obj_at_midpt) ; In upper half (bin-search obj vec (+ midpt 1) end))))))

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt37 Multi-Dimensional Arrays  MAKE-ARRAY - Generates arrays  Has keywords :initial-element and :initial-contents > (setf arr (make-array '(2 3))) #2A((NIL NIL NIL) (NIL NIL NIL)) > (setf vec (make-array 4 :initial-element 1)) #( ) ; A Vector is a 1-dimensional array; note keyword parameter above > (setf tic-tac-toe (make-array '(3 3) :initial-contents '((- O O) (- X X) (- X -)))) #2A((- O O) (- X X) (- X -))  Aref used to reference multi-dimensional arrays > (setf (aref tic-tac-toe 0 0) 'O)

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt38 Strings and Characters  String - A vector of characters  Character - Denoted using #\ > (setf x "abcde") "abcde" > (aref x 2) #\c > (setf (aref x 2) #\z) #\z > x "abzde"

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt39 User Defined Structures  Association lists were used to represent data (setf lisp-course-info '( (course-number CSCI221001) (credit-hours 1) (name (Programming in Lisp)) (instructor (Alok Mehta)) (department (Computer Science))))  A more efficient - but less flexible way:  User Defined Structures > (defstruct course ; The structure name course-number ; The fields of the structure (credit-hours 4) (name (progn (format t "Course Name: ") (read))) instructor department) COURSE Dflt Value = 4 Default Value = Prompt user

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt40 Defstruct  Defines a structure with given name and fields  Automatically writes code to define functions  Constructors, type-checking –make-course, course-p, copy-course  Field access/update –course-course-number, course-credit-hours, course-name, course- instructor, course-department  An example of a function that generates functions  There is no equivalent analogy for C/C++, Java, etc.  You can write functions like Defstruct  CLOS - Common Lisp Object System

CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt41 Using Course Structure  Examples of using the course defstruct > (setf c2 (make-course :credit-hours 1 :name '(Programming in Lisp))) #S(COURSE :COURSE-NUMBER NIL :CREDIT-HOURS 1 :NAME (PROGRAMMING IN LISP) :INSTRUCTOR NIL :DEPARTMENT NIL) > (setf c1 (make-course)) ; Uses default values Course Name: (Programming in C++) #S(COURSE :COURSE-NUMBER NIL :CREDIT-HOURS 4 :NAME (PROGRAMMING IN C++) :INSTRUCTOR NIL :DEPARTMENT NIL) > (setf (course-instructor c2) '(Alok Mehta)) (ALOK MEHTA)