From Graham's ANSI Common Lisp: Ch.2: 7-9; Ch.3: 1,3,5; Ch. 4: 1,2,4.
LET
, DO
, etc.),
define a function that takes a list as an argument and retursn true if one
of its elements is a list.
2.8 Give iterative and recursive definitions of a function that:
(a) takes a positive integer and prints that many dots.
(b) takes alist and returns the number of times the symbol a
occurs in it.
2.9 A friend is trying to write a function that returns the sum of all the non-nil elements in a list. He has written two versions of this function, and neither of them work. Explain what's wrong with each, and give a correct version:
(a) (defun summit (lst) (remove nil lst) (apply #'+ lst)) (b) (defun summit (lst) (let ((x (car lst))) (if (null x) (summit (cdr lst)) (+ x (summit (cdr lst))))))3.1 Show the following in box notation:
- (a b (c d))
- (a (b (c (d))))
- (((a b) c) d)
- (a (b . c) . d)
3.3 Define a function that takes a list and returns a list indicating the
number of times each (using eql
test) element appears, sorted from most
common element to least common:
> (occurrences '(a b a d a c d c a)) ((A . 4) (C . 2) (D . 2) (B . 1))3.5 Suppose the function
pos+
takes a list of returns alist of each element plus its position:
> (pos+ '( 7 5 1 4)) (7 6 3 7)Define this function using (a) recursion, (b) iteration, (c)
mapcar
.
4.1 Define a function to take a square array and returns the array, rotated 90 degrees clockwise. You'll need array-dimensions
(pg. 361).
4.2 Read the description of reduce
on page 368 and 69, then use it to define:
copy-list
reverse
(for lists)