CIS 400 Lecture 11

 

Continuing with simulated Lisp output:

 

>(+  2  3)

5

>(float  2)                              ; changes inter 2 to float

2.0

>(1+  3)                                ; similar to increment in C++

4

>(setq  x  3)    

3

>(1+  x)

4                                                                   ; "x" is STILL 3, parameter transmission is by VALUE

>(mod  5  2)

1                                                                   ; like modulo, or "%", in C++

 

;;;;;;;;;;;;;;;  an expression like  2 * 2 + 4 / 5  in Lisp is:  ;;;;;;;;;;;;;;;;;

 

>( +  ( *  2 2 )  ( /  4  5 ) )

 

;;;;;;;;;;;;;;  list manipulations in Lisp  ;;;;;;;;;;;;;;;;;;;;;

 

>(  car  '(a  b  c))                     ;"car" is "first" in common Lisp implementation

a

>(setq  L '(a  b  c))                  ; list is unchanged

>(first L)

a

>(cdr  '(a b c))                           ; list is unchanged

(b  c)

>(car  (car  '(a  b  c)))

error-can't take "car" of "a", it's not a list                       

>(car  (car '((a  b) c)))

a

 

;;;;;;;;;;;;;;;;;;;  OR  ;;;;;;;;;;;;;;;;;;;;

 

>(caar  '(a b c))                         ;"caar" is same as "(car  (car "

error-can't take "ca"r of "a", it's not a list

>(cadr  '(a  b)  c))                     ;"cadr" is same as "(car  (cdr "

c

>(cadr  '(a  b c))

b

>(list  'a  'b  '(c  d))

(a  b  (c  d))

 

>(car  (list 'a  'b  '(c  d))               ; passing output of one function as input to another

a

>(setq L  (list 'a  'b  '(c  d)))

>(cadr  L)                                   ; "cdr" evaluated first, then "car". So first is (b  (c  d)), then b

b

>(cons 'a  '(b  c))                         ; "cons" is like concatenate

(a  (b  c))

>(append  '(a  b)  '((c  d)  e))       ; note difference from 'cons'

(a  b (c  d)  e)

 

"cons" comes from "cons cell"

 

>(reverse  '(a  b  (c  d)  e))           ; this effects "top level" cells ONLY, c & d not reversed

(e  (c  d)  b  a)

>(sort  '>  '(a b  w  z  y))

(a  b  w  y  z)

>(length  '(a  b  (c  d)  e))              ; top level cells only

4

>(last '(a  b  (c d))

(c  d)

>(subst 'a  'b  '(a  b  c  b))             ; find FIRST b and substitute with a, top level only

(a  a  c  b)

>(eval  (cdr  '(a  +  2 3)))              ; "force" evaluation of  "cdr" or (+ 2 3)

5

>(setq  a  'b)

b

>(eval a)                                        ; apostrophe needed, run time error

unbound variable error

>a

b

>(eval  'a)

b

>(setq  b  3)

>(setq  a  'b)

b

>(eval  (eval  (a)))

3

 

 

 

Defining functions in Lisp

 

>(defun intro  (x  y)                           ; "defun" is for defining functions

     (list x  'this  'is  y)                         ; x & y are not quoted, we want them evaluated

   )

 

>(intro  'Stanley  'Livingston)

(Stanley this is Livingston)

 

Predicate functions

 

Returns either "t" or "nil" for true or false, (empty set)

 

>(listp  '(a  b  c))                                ; is this a list?

T

>(listp  '3)

nil

>(atom  '(a b  c))                               ; is this an atom?

nil

>(atom 3)

t                                                       ; also functions for constp, floatp, integerp, realp

>(>  2  3)                                         ; is 2 greater than 3?

nil