Assignment 2
Library file function definitions:
- equalp, eql
- length
- 1+, 1-
- abs
- elt, nth
- first, second, third, cadr, rest
- integerp - hack, always returns true
- remove
Problems:
- mainly uncompleted, hopefully will not have to be
- setf replaced with setq
Problem 1
Function: intersection
uses iteration to compute the set intersection of two lists.
The set intersection of two lists is the list of element that occur
in both lists. Note that we ignore the order of elements in a list
as far as set intersection is concerned. Also an element only appears
in the result one, even if it appears repeatedly in the arguments.
Loop through the first list. If it is in the 2nd list, add it to the common
list if it hasn't been already. Return the common list.
Note - no need to do a double loop, ie through list1 and list2
Problem 2 (thanks to Paul Harrison)
First define some utilities for dealing with lists of lists
; Transpose-rec gets the car of all the sublists and makes a list of them,
; then applies itself to the remainder
Problem 3
uses global variable *cardatabase* to store a list of cars
add-car is a function which allows us to add car properties to a symbol and
add that symbol to our list of cars.
Look at the cost property to find the cheapest car
Problem 4
(a) Comment the most function.
this function returns the element of a list with the
highest winning score, according to some scoring function.
It returns two values, the winning element, and its score.
(b) Scoring functions:
These functions aren't very intelligent - they don't use a loop to scan
the list but rather they access each element explicitly. This actually
takes fewer lines of code than a loop would have, but I would rewrite
these functions if extensibility was required.
(c) Input to interpreter:
(d) A version of most that uses apply instead of funcall:
Replace instances of (funcall fn arg) with (apply fn (list arg))
this function returns the element of a list with the
highest winning score, according to some scoring function.
It returns two values, the winning element, and its score.
Problem 5a
(defun summit1 (lst)
(remove '() lst)
(apply #'+ lst))
This function does not work on lists that contain NILs, because the
remove function is nondestructive - it returns a list of the desired
configuration, but does not alter its parameter. To fix this, the
remove expression should be made the parameter of apply:
Note that you could use the delete function instead of remove, becuase
that is destructive, but it is generally considered bad programming practice
to use destructive functions
Problem 5b.
This is a recursive function without a terminating condition, and therefore
it overruns the stack when run. The function needs to return zero when
it is passed an argument of length zero, and this can be done most simply by
inserting a conditional statement.
Problem 6
The problem with the original version of this code was that the recursive
call was appending a permutation of the rest of the original parameter
list (var), instead of the list containing the remaining list elements
(var2). Also, (<=n 0) need to be changed to (<= n 0).
-------------------------------------------------------------------------
Problem 7 (thanks to Paul Harrison)
This is pretty nifty! Apply takes 2 arguments: the first a function (mapcar
in this case), the second, a list of actual arguments to that function.
So if L is ((a b) (c d) (e f)), then mapcar gets called as
(mapcar 'list '(a b) '(c d) '(e f))
Recall that the first argument to mapcar is the function to be applied.
The other arguments are all lists. The first time, the 'list function is
applied to the first element of each of the lists e.g. (list 'a 'c 'f)
returning (a c f). Etc!
-------------------------------------------------------------------------
Problem 8
The 3 lines were:
(format t "~%")
(compress '(1 1 1 0 1 0 0 0 0 1))
(format t "problem 8, (compress '(1 1 1 0 1 0 0 0 0 1)), ~%problem 8, ~a~%"
(compress '(1 1 1 0 1 0 0 0 0 1))
but I'm pretty sure this interprter will not have format
Problem 9
This function was supplied.