Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
-
Upload
august-greer -
Category
Documents
-
view
217 -
download
3
Transcript of Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
Basic Lisp
CIS 479/579
Bruce R. Maxim
UM-Dearborn
What is Lisp?
• A functional language includes linked lists as a built-in data type
• Everything in lisp is either an atom or a list (expression)
• Lisp is dynamically typed• It is possible to define executable data
structures because data and code are equivalent
Lists and Atoms
• Consider the linked list ‘(a b c)a is an atomb is an atomc is an atom
• Atoms have both names and values• Often times in Lisp programming you
will work with the name and ignore the value
Literal and Numeric Atoms
• Numeric atoms – (name matches value)
12 or –1.53
• Literal atoms – begin with letter– value is undefined when created
a or Sam
Pre-Defined Literal Atoms
• T or t – True (logical constant)
• Nil or nil – Nil = ‘( ) meaning the "empty list“– False (logical constant)
• There are no reserved words in Lisp to that means t or nil can be redefined by the user
Using Lisp
• Lisp is a interactive system• You can files to be processed as a batch file,
but more often than not the programmer is the “main program”
• Every expression typed a the “>” prompt is “read” and “evaluated” unless it is prefixed with an apostrophe ‘
• Typing (exit) at the “>” prompt terminates the xlisp program
Sample Output>(a b c)error: unbound function - aif continued: try evaluating symbol again1> [ back to top level ]> '(a b c)(a b c)> 11> 1.21.2> aerror: unbound variable - aif continued: try evaluating symbol again1> [ back to top level ]
Sample Output> nilnil> tt> Tt> '(a (b c) d)(a (b c) d)> (setq sam 'abc)abc> samabc
Arithmetic Functions> (/ 2 3)2/3> (/ 1.0 2)0.5> (1+ 3)4> (mod 2 3)2> (mod 5 2)1> (+ (* 2 2) (/ 4.0 5))4.8
car=first and cdr=rest> (car '(a b c))a> (cdr '(a b c))(b c)> (car nil)nil> (cdr nil)nil> (first '(a b c))a> (car (cdr '(a b c)))b> (cadr '(a b c))b
List Functions> (list 'a 2 'b)(a 2 b)> (list '(a b) '(c d))((a b) (c d))> (list sam c)error: unbound variable - cif continued: try evaluating symbol again1> [ back to top level ]> (list sam 'c)(abc c)> (cons 'a '(b c d))(a b c d)> (cons '(a b c) 'd)((a b c) . d)
List Functions> (append '(a b) '(c d))(a b c d)> (reverse '(a b c d))(d c b a)> (length '(a (b c) d)))3> > (last '(a b c d))(d)> (subst 'a 'b '(a b c))(a a c)> (subst 'a 'b '(a b c b))(a a c a)
eval and quote> (eval (cdr '(a + 2 3)))5> (setq a 'b)b> ab> berror: unbound variable - bif continued: try evaluating symbol
again1> [ back to top level ]> (set 'a 'b)b> (eval (eval ''a))b> 'aa
eval and quote> (eval (eval '(quote a)))
b
> 'a
a
> (eval '(list '* 9 6))
(* 9 6)
> (eval (eval '(list * 9 6)))
error: bad function - (* 9 6)
1> [ back to top level ]
> (eval (eval '(list '* 9 6)))
54
Function Definition
> (defun intro (x y)
(list x 'this 'is y)
)
Intro
>; be careful not to quote the arguments when
>; defining the function
> (intro 2 3)
(2 this is 3)
> (intro 'stanley 'livingston)
(stanley this is livingston)
Predicate Functions
> (atom 2)t> (atom '(a b c))nil> (listp 2)nil> (listp '(a b c))t> (equal 2 3)nil> (= 2 3)nil> (equal 6 (* 2 3))t
Predicate Functions> (set a ‘(1 2))(1 2)> (equal a ‘(1 2))t> (eql a ‘(1 2))nil> (null '())t> (null 2)nil> nilnil> (null nil)t
Membership Functions
> (member 'c '(a b c d))
(c d)
> (member 'a '((a b) c d))
nil
> (member '(d e) '((a b) c (d e) f))
nil
> (assoc 'c '((a b) (c d) (e f)))
(c d)