Keys to Lisp One
Answers
CIS447
Pieces of the fclean function:
Whether or not you plan to write the fclean function, the information here is important to the proper use of the full power of Lisp.
Key Functions:
atom checks whether or not its argument is an atom.
listp checks whether or not its argument is a list.
Equality predicates:
eq, equal, equalp, eql, =
Notice the results of the following Lisp forms:
(eq a a) ฎ t
(equal a a) ฎ t
(eq a b) ฎ nil
(equal a b) ฎ nil
(eq (a) (a)) ฎ nil
(equal (a) (a)) ฎ t
member checks to see if its first argument is an element of its 2nd argument.
member with :test uses specified membership test.
Exercise:
Write the following function (test):
neg-of:
(neg-of x y) t, if x = y
nil, otherwise
(defun neg-of (x y)(= x (- 0 y)))
Write the following function using neg-of:
neg-member:
(neg-member elt list) t, if elt ฮ list, where elt and all the elements of list are integers.
nil, otherwise
(defun neg-member
(elt list)(member elt list :test #neg-of))
Write the following function using neg-member:
neg-intersection:
(neg-intersection list1 list2) returns those elements of list1 which are the negation of some element in list2, where the elements of both lists are integers.
Exactly the same form as setint, but using neg-member.
let, let* used to establish local variables
Exercise:
Write the following function:
pospogressionp:
(posprogressionp list) t, if for all i, elti < elti+1, where all the elements of list are integers and where elti and elti+1 are the ith and i+1st positive integers of list.
nil, otherwise
(defun posp (n)(> n 0))
(defun posvals-of (list)
(cond ((endp list) nil)
((posp (first list))
(cons (first list)(posvals-of (rest list))))
(t (posvals-of (rest list)))
))
ppgp = posprogressionp
(defun ppgp (list)(ppgp-aux (posvals-of list)))
(defun ppgp-aux (poslist)
(or (endp poslist)
(endp (rest poslist))
(and (< (first poslist)(second poslist))
(ppgp-aux (rest poslist))
)))
Write the following function using progressionp and let:
posprogressions-of:
(posprogressions-of lolist) returns a new list consisting of all the posprogressions of lolist, where lolist is a list whose elements are lists of integers, and where a pospogression contains only positive integers.
(defun ppgs-of (lolist)
(if (endp lolist)
nil
(let ((poslist (posvals-of (first lolist))))
(if (ppgp poslist)
(cons poslist (ppgs-of (rest lolist)))
(ppgs-of (rest lolist))
))))
(setf x ((1 2 3)(3 2 1)(-1 2 -3 4 -2 5)(-5 1 2 -3 4 3)))
(ppgs-of x) ฎ ((1 2 3)(2 4 5))