Lisp: Local Environments. The following allow control of local environments (similar to blocks in other languages) Let

Lisp: Iteration • Do -Lisp repetition – Syntax (do init-list test-list body ) init-list: ((atom [init-value [step]]) ( ... ) ... ) test-list: (predica...
Author: Lauren McDaniel
19 downloads 1 Views 98KB Size
Lisp: Iteration • Do -Lisp repetition – Syntax (do init-list test-list body ) init-list: ((atom [init-value [step]]) ( ... ) ... ) test-list: (predicate forms) – Semantics 1. On entry, atoms in init-list bound to init-values If init-list empty, NIL 2. Predicate in test-list evaluated If non-NIL, evaluate forms and return last evaluated if NIL, evaluate body

1

Lisp: Local Environments • The following allow control of local environments (similar to blocks in other languages) • Let – Syntax (let init-list body) init-list is a list of atoms and/or pair lists – Semantics 1. init-list is evaluated If element is atomic, is bound to NIL If element is list, 2nd element is evaluated and bound to first 2. All components of init-list evaluated in parallel - no dependency (Use let* instead) 3. Body of let evaluated 4. Value of last evaluated form of body returned – Atoms act as local variables for the let body • Prog – Syntax (prog init-list body) init-list and body same as for let – Semantics 1. Bind atoms in init-list same as for let 2. Evaluate body 3. Return NIL – Most primitive form of iteration ∗ Isolated atoms in body serve as labels ∗ (go label) is goto function ∗ (return s-expression) produces return value – Variations: prog1, prog2, progn (Return values of 1st, 2nd, and last s-expressions)

2

Lisp: Additional Iteration • Dotimes – Syntax (dotimes (lcv init-val [return-val]) body) lcv is unevaluated atom - typical loop control variable init-val must evaluate to an integer – Semantics 1. lcv initialized to init-val 2. body executed init-val times (lcv assumes values 0 thru init-val - 1) 3. Return return-val, or NIL • Dolist – Syntax (dolist (var list [return-val]) body) – Semantics 1. 2. 3. 4. 5.

var initialized to (car list) body executed list ← (cdr list) Iterate until (null list) true Return return-val, or NIL

3

Lisp: Case • Another effort to conform to user-friendly traditional programming constructs • Syntax (case selector (options forms) (options forms) ... [(default forms)]) • Semantics 1. selector evaluated 2. options processed – If an atom, compare with selector – If a list, check for selector’s membership in list 3. On first non-nil result, evaluate associated forms 4. Optional default case must be last default may be T or otherwise 5. Return last expression evaluated

4

Lisp: Mapping Functions • These are iterators – General Syntax (map-fn-name applied-fn lists) lists must have same numbers of elements Must be 1 list for each argument of applied-fn – Semantics 1. applied-fn is applied to appropriate part of each list 2. lists are uniformly cdred 3. Iterate until (null list) true • Variations: – Mapcar applied-fn applied to cars of lists List of results returned – Mapc Same as mapcar, but returns 2nd argument – Maplist applied-fn applied to cdrs of lists List of results returned – Mapl Same as maplist, but returns 2nd argument

5

Lisp: Evaluation Functions • These control evaluation of s-expressions • Allow construction of functions on-the-fly i.e., self-modifying code • They emphasize point that programs and data are essentially the same in lisp • Eval – Syntax (eval form) – Semantics 1. form evaluated 2. eval causes execution of result • Apply – Syntax (apply fn-name arg-list) arg-list is a list of parameters to fn-name – Semantics 1. Function fn-name applied to arguments in arg-list • Funcall – Syntax (funcall fn-name args) – Semantics ∗ Same as for apply, except args not represented as a list

6

Lisp: Function Parameters • Note that Lisp has several types of functions: – ”Standard” functions like car Parameters evaluated and function applied – ”Special” functions like setq Some parameters not evaluated – Others like + which can take an indefinite number of parameters • Lisp provides facilities for creating these variations These all are placed after the mandatory parameters 1. & optional – – – –

Used to specify optional parameters Syntax: & optional symbol | (symbol def ault [f lag-symbol]) ... symbol is optional argument def ault is value assigned to symbol if argument not supplied nil if def ault not supplied – f lag-symbol set to T if symbol supplied

2. & rest – – – –

Used as catchall Appears after optionals Syntax: & rest symbol symbol bound to list of all additional arguments that follow specified formals

3. & key – – – –

Allows selective passing of parameters Syntax: & key symbol | (symbol def ault) ... Works much like & optional symbol followed by colon in call, followed by value

7

Lisp: Destructive Functions • These cause changes to arguments - all discussed so far do not • Useful in that do not generate lots of cons cells • Dangerous because arguments are permanently altered • Rplaca – Syntax (rplaca cell object) – Semantics 1. Returns result of replacing car of cell with object – Cell is permanently changed • Rplacd – Syntax (rplacd cell object) – Semantics 1. As in rplaca, using cdr • Nconc – Syntax (nconc lists) – Semantics 1. Returns result of appending lists – First list is permanently changed • Mapcan - destructive version of mapcar • Mapcon - destructive version of maplist • Setf – Syntax (setf location value) – Semantics 1. Sets the value of location to value – Most general assignment function – Can be used for any kind of location in Lisp 8

Lisp: Property Lists • Property list is a list of property-value pairs • Associated with atoms (along with print name, function, etc.) • Get – Syntax (get symbol property [null-atom]) – Semantics 1. Returns value associated with property in symbol’s property list 2. If property not in property list, return NIL If optional null-atom appears, it is returned instead of NIL • Symbol-plist – Syntax (symbol-plist symbol) – Semantics 1. Returns entire property list of symbol • Remprop – Syntax (remprop symbol property) – Semantics 1. Deletes property from symbol’s property list 2. Returns T if property exists, NIL otherwise • Properties set using setf: (setf (get symbol property) value)

9

Lisp: Association Lists • Similar to property lists, but NOT stored with symbols • List of dotted pairs • Acons – Syntax (acons key value alist) – Semantics 1. Adds key-value dotted pair to alist • Pairlis – Syntax (pairlis list1 list2 [alist]) – Semantics 1. Returns association list pairing elements from list1 and list1 2. If optional alist included, returns pairs as above added to alist • Assoc – Syntax (assoc key alist) – Semantics 1. Returns first dotted pair in alist whose car eql key • Rassoc – Syntax (rassoc key alist) – Semantics 1. Same as assoc, but does comparisons on cdr of pairs • Major differences between property and association lists: 1. Property lists altered destructively, association lists are not 2. Property list properties appear 1X, association list keys may occur multiple times 3. Property lists are associated with symbols, association lists are not 10

Lisp: Symbol Generation • Often need lots of atoms to represent information on an as-needed basis • Not convenient to define such atoms ahead of time • Lisp provides means of generating symbols on-the-fly – Gensym ∗ Syntax (gensym [x]) ∗ Semantics 1. Returns an uninterned system-generated symbol guaranteed to be unique 2. Format is #:Ginteger 3. If x is supplied as a string, it becomes the prefix of subsequent symbols 4. If x is supplied as a number, it resets the counter to that number 5. The #: signifies an uninterned symbol 6. Uninterned symbols are not part of any packages 7. Symbols generated by gensym are generally intended to be used internally by a program – Gentemp ∗ Syntax (gentemp [x]) ∗ Semantics 1. Same as for gensym except that symbol is interned in current package 2. Format is #:Tinteger 3. If x is supplied, it is not persistent

11

Lisp: IO • Since all Lisp functions return something, these usually not needed except for prompts, file io, etc. – Prin1 ∗ Syntax (prin1 object [stream]) ∗ Semantics 1. Prints object to a stream 2. Default stream is monitor 3. Includes all special characters 4. Returns object 5. Value printed is readable by read – Print ∗ Syntax (print object [stream]) ∗ Semantics 1. Same as prin1, except preceded by newline and followed by space – Princ ∗ Syntax (princ object [stream]) ∗ Semantics 1. Same as prin1, except omits special characters – Terpri ∗ Syntax (terpri [stream]) ∗ Semantics 1. Issues a line feed 2. Returns nil

12

Lisp: IO (2) – Write-line ∗ Syntax (write-line string [stream]) ∗ Semantics 1. Prints string on a single line 2. Returns string – Format ∗ Syntax (format stream string [objects]) ∗ Semantics 1. Prints string to stream 2. Stream t send to screen 3. Like C, string may have embedded formatting characters (each preceded by ∼ 4. See resources for formatting characters – Open ∗ Syntax (open filename :direction direction) ∗ Semantics 1. Opens f ilename for access, where f ilename is name of physical file 2. direction can be : input, : output, : io 3. Returns symbolic link to file – Close ∗ Syntax (close file) ∗ Semantics 1. Closes symbolic file f ile – Dribble ∗ Syntax (dribble filename) ∗ Semantics 1. Sends screen output to physical file f ilename 13

Suggest Documents