130 likes | 401 Views
Common LISP. Omar Haque Csci169 Spring 2003. Representing Data. Values represented by symbolic expressions S-expressions Atom: a string of characters Ex: I THISISAATOM 1212 List: seq of atoms or lists Ex: (ADD A B) ((MEAT CHICKEN)(APPLE ORANGE PEAR)WATER) Empty list ( ) is NIL.
E N D
Common LISP Omar Haque Csci169 Spring 2003
Representing Data • Values represented by symbolic expressions • S-expressions • Atom: a string of characters • Ex: I THISISAATOM 1212 • List: seq of atoms or lists • Ex: • (ADD A B) • ((MEAT CHICKEN)(APPLE ORANGE PEAR)WATER) • Empty list ( ) is NIL
Representing Data • Atom is either a number ( r value) or a name(l value) • Binding • SET binds value globally • (SET X (A B C)) • LET binds it locally
Identity Function • QUOTE returns its single argument as its value • It prevents the evaluation of an atom, and the l-value is returned NOT the r-value • Kind of like using constants • Ex:(‘(SOMETHING) • Result:(SOMETHING)
Functions • Arguments are evaluated first and then the function is applied to their results Ex: (CAR ‘(A B C)) Result: A • NOTE: we use the ‘ to prevent evaluation • CAR returns first element of a list • CDR returns the list without the 1st element
Remember • An atom is different than a list • Ex: (CDR ‘(B)) returns NIL • If we try: • (CDR ‘A) returns Error: A is not of type LIST. • CONS function • Adds an element to the front of a list • Ex: (CONS ‘(A T O M) ‘(L I S T)) • Returns ((A T O M) L I S T) • (A T O M) is a single element of the list which has 5 elements
Predicates • T denotes true • NIL denotes false • Examples of Predicate functions: • (ATOM ‘A) • (EQ ‘A ‘B) returns NIL
Case Expression • COND takes arguments of pairs of (predicate, expression) • Returns the value of the expression for the 1st matching predicate Ex: (COND ( (ATOM ‘(X) ‘B) (T ‘C) )) returns C
Function Definition • Based on lambda expression • function λx,y.x+y • LISP (LAMBDA (X Y) (PLUS X Y)) • Binding name to function using DEFUN • (DEFUN (SQUARE (X) (* X X)) • Now you can use SQUARE function • Ex:
Loading File in gcl • Type code into mystery.lisp • (load “mystery.lisp”) returns T • (trace mystery) to see a trace of a function call • Ex:
Comparison with Schema • http://www-inst.eecs.berkeley.edu/~sho/cs188/lab_lisp • Comments begin with ; • Math operators work like in Schema (in lecture notes) • Ex: (+(+ 2 3)6)
More on LISP • http://www.cs.utexas.edu/users/novak/schemevscl.html • http://www.seas.gwu.edu/~seascf/unix/doc/gcl/gcl-si_toc.html • Can find most of what you need here • http://www.netaxs.com/~nerp/lisp/lisp-morefun.html • http://www.apl.jhu.edu/~hall/lisp.html