Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

18
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn

Transcript of Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

Page 1: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

Basic Lisp

CIS 479/579

Bruce R. Maxim

UM-Dearborn

Page 2: 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

Page 3: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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

Page 4: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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

Page 5: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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

Page 6: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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

Page 7: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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 ]

Page 8: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

Sample Output> nilnil> tt> Tt> '(a (b c) d)(a (b c) d)> (setq sam 'abc)abc> samabc

Page 9: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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

Page 10: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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

Page 11: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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)

Page 12: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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)

Page 13: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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

Page 14: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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

Page 15: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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)

Page 16: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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

Page 17: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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

Page 18: Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.

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)