1 / 12

Common LISP

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.

gad
Download Presentation

Common LISP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Common LISP Omar Haque Csci169 Spring 2003

  2. 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

  3. 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

  4. 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)

  5. 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

  6. 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

  7. Predicates • T denotes true • NIL denotes false • Examples of Predicate functions: • (ATOM ‘A) • (EQ ‘A ‘B) returns NIL

  8. 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

  9. 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:

  10. 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:

  11. 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)

  12. 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

More Related