Assignment 2, Lisp Notes and Review, Due September 10, 2007
Functions:
· (car <list>)
o (car (1 2 3)) => 1
· (cdr <list>)
o (cdr (1 2 3)) => (2 3)
o (cdr (1)) => NIL
· (list <element1 element2 element3 ….. elementN>)
o (setf fullhouse (list 2 2 3 3 3)))
· (setf <variable [list or atom]> <value>)
o (setf eat 8)
o (setf eatthis (list pie cake))
· (length <list>)
o (length fullhouse) => 5
· (defun <function name> <arg list> (exp1) (exp2) (exp3) ….(expN) )
· (cond ( (condition 1) (exp1)..(expN)) ( (condition 2) (exp1)..(expN)) (t (exp1)..(expN)) )
· (append (list1) (list2))
o (append (list 1 2 3) (list 4 5)) => (1 2 3 4 5)
· (cons (list1) (list2))
o (cons (list 1 2 3) (list 4 5)) => ((1 2 3) 4 5)
· (eval (list))
o (eval (car (1 2 3)) => 1
Examples:
(defun init ()
(setf big (list 1 2 3 4 5 6 7 8 9 10))
(setf fullhouse (list 2 2 2 3 3))
)
(defun lastone (list)
(cond
(( = (length list) 1)
(first list)
)
( t
(print list)
(lastone (cdr list))
)
)
)
(defun sumList(addlist)
(cond
((= (length addlist) 1)
(car addlist)
)
(t
(+ (car addlist)(sumList(cdr addlist)))
)
)
)
(defun average (list)
(/ (sumList list) (length list))
)
(defun middleNum (nums)
(setf mid (/ (length nums) 2))
(getNum mid nums)
)
(defun getNum (mid nums)
(cond
((= 1 mid)
(car nums)
)
(t
(getNum (- mid 1) (cdr nums))
)
)
)
(defun insultMe (message)
(append (list 'Yo 'Mamma) message)
)
- Write a function that reverses a list.
- (revMe (list 1 2 3)) => (3 2 1)
- Wirte a function that counts the number of items in a list, do not use the length function.
- Write a factorial function that uses recursion.
- (fact (3)) => 6
1