CS2360 Lab 3, Tuesday January 24
This lab is heavily based on Heather Stehman's Original Lab 4.
Announcements:
- Sign-in
- Turn in codes if you have them
FUNCALL
Type the following in and note the results in the blanks:
- (funcall #'+ 1 2 3 4 5) __________________________
- (funcall #'cons 'a '(b c)) __________________________
- (funcall #'max '3 5 2 7) __________________________`
Now suppose that one day, in a rather wicked mood, your boss asked to write a suite of functions
that would replace every occurrence of something with something else. This is key/value
substitution but rather based on some predicate. For instance, using only these functions:
defun, cond, null, first, cons, eq, eql, equal, rest, append, quote ('),
and, or, not and the predicates listed beside each one below, respectively:
- (defun replace-all-atoms (inlist withwhat) ... ) atom
- (defun replace-all-lists (inlist withwaht) ... ) listp
- (defun replace-all-negative (inlist withwhat) ... ) negativep
- (defun replace-all-positive (inlist withwhat) ... ) positivep
- (defun replace-all-zeroes (inlist withwhat) ... ) zerop
- (defun replace-all-integers (inlist withwhat) ... ) integerp
Just do it.
Write up these six functions, built out of one (more general) function called general-replace. The
six functions above should simple calls to general replace. This function should be organized
something like this:
(defun general-replace (mylist element predicate)
(if (null mylist) nil
(if (funcall predicate (first mylist))
;;; do some stuff to handle replacing the element
;; do some stuff that doesn't replace the element )))
You make want to make element be 'replaced when you are debugging so you can test your code.
Further, I found #'zerop was nice to debug with (but don't forget the tracer and stepper from last
week!).
Same old, same old...
So, now your boss comes in and he want some more of these functions written, in this case they are:
- (defun replace-all-girls (inlist withwhat) ... )
- (defun replace-all-boys (inlist withwhat) ... )
- (defun replace-all-animals (inlist withwhat) ... )
- (defun replace-all-humans (inlist withwhat) ... )
What's up with that stuff? There is no girlp to help you out of this mess! Further, how do I tell if
Tracy is a boy or a girl? Ok, to make it easier, we'll label all the elements of the list like this:
((tracy male) (bob male) (lisa female) (fido animal))
Now, this wouldn't be a problem except that (whine, whine) we have to write a predicate
for each one... No you don't! Lambdaman to the rescue! You can use the lambda "function."
Here's an example without the lamda function:
(defun replace-all-girls (inlist withwhat)
(general-replace inlist withwhat #'girlp))
(defun girlp (someone) ... )
Here it is with lambda function:
(defun replace-all-girst (inlist withwhat)
(general-replace inlist withwhat
#'(lambda (someone) ..test for it here ... )))
Play it, Sam!
You guessed it, you need to write up these functions by using lambda expressions. I would suggest
that you abstract away (use a function for this!) the check for the type of the creature-specifier
(female, animal, male). It may seem silly now but, consider a "hot-tip"...
Didn't Trust Me, Didja
Now your boss has changed his mind, he is going to change the data structure for the names and
the type of creature... Now he is going to do it this way:
((male tracy) (male bob) (female lisa) (animal fido))
Modify you solution to the previous portion to handle this new data structure. If you listened to me
the first time, you should only have to modify your code in one small place, not in four different
ones.
MAPCAR
Try doing some experiments with the mapcar function explained in class:
(mapcar #'atom '(a b c (d e))) ________________________
(mapcar #'1- '( 1 2 3 4 5)) ________________________
(mapcar #'(lambda (alist) (append (first alist) (second alist)))
'((a b)(c d)(e f)(g))) ________________________
Make sure you walk through why the last one does what it does. E.g. How does the variable alist
get used?
Rewrite the general-replace function from above using mapcar and a lambda expression.
Ian Smith (iansmith@cc.gatech.edu)
Last Modified 23 Jan 95 by Ian Smith (iansmith@cc.gatech.edu)