编程代考 Tutorial 7: LISP exercises

Tutorial 7: LISP exercises

1. Write a function, zap, which given two arguments, an atom and a list, add the atom in-between all the elements in the list as shown below:

· (zap ‘x nil)
()

· (zap ‘x ‘(1 2 3 4))
(x 1 x 2 x 3 x 4)

2. The set function, adjoin, adds a new element to the list if it is not there. Write your own adjoin function and called it my-adjoin (hint: use member):

· (my-adjoin ‘a ‘(b c d a))
(b c d a)

· (my-adjoin 2 ‘(3 4 5))
(2 3 4 5)

3. Write a function, even-greater-n, which given a number and a list, return the first even number greater than n:

· (even-greater-n ‘10 ‘(1 2 3 4 6))
nil

· (even-greater-n 2 ‘(3 4 5))
4

4. f

(defun even-greater-n (n L)
(cond ((null L) nil)
((and (> (car L) n) (evenp (car L))) (car L))
(t (even-greater-n n (cdr L)))))

(defun my-adjoin (n L)
(cond ((member n L) L)
(t (cons n L))))

(defun zap (x L)
(cond ((null L) nil)
(t (append (list x (car L)) (zap x (cdr L))))))