;;;; Section 1.7 ;;;; ;Adam Nelson ;Chet Tobrey (defun mappend (fn the-list) "Apply fn to each element of list and append the results." (apply #'append (mapcar fn the-list))) (defun self-and-double (x) (list x (+ x x))) (defun number-and-negation (x) "If x is a number, return a list of x and -x." (if (numberp x) (list x (- x)) nil)) (defun numbers-and-negations (input) "Given a list, return only their numbers and negations" (mappend #'number-and-negation input)) (defun mappend2 (fn the-list) "Apply fn to each element of list and append the results" (if (null the-list) nil (append (funcall fn (first the-list)) (mappend2 fn (rest the-list))))) (egs :1.7 (eg '(apply #'+ '(1 2 3 4)) :of "Applies the addition function to a list of four numbers" :out '10) (eg '(apply #'append '((1 2 3) (a b c))) :of "Applies append to the two lists 123 and abc" :out '(1 2 3 a b c)) (eg '(self-and-double 3) :of "Calls self-and-double with parameter 3" :out '(3 6)) (eg '(apply #'self-and-double '(3)) :of "Applies self-and-double and the parameter 3" :out '(3 6)) (eg '(mapcar #'self-and-double '(1 10 300)) :of "Uses mapcar with the self-and-double function" :out '((1 2) (10 20) (300 600))) (eg '(mappend #'self-and-double '(1 10 300)) :of "Uses the self-and-double function with the mappend function - same as above but appends results" :out '(1 2 10 20 300 600)) (eg '(numbers-and-negations '(testing 1 2 3 test)) :of "Testing the numbers and negations function" :out '(1 -1 2 -2 3 -3)) (eg '(funcall #'+ 2 3) :of "Using funcall to add two numbers" :out '5) (eg '(apply #'+ '(2 3)) :of "Using apply to add two numbers" :out '5) (eg '(funcall #'(lambda (x) (+ x 2)) 4) :of "Tests the funcall function with a lambda function" :out '6) (eg '(mapcar #'(lambda (x) (+ x x)) '(1 2 3 4 5)) :of "Uses mapcar with a lambda function" :out '(2 4 6 8 10)) (eg '(mappend2 #'(lambda (l) (list l (reverse l))) '((1 2 3) (a b c))) :of "Uses mappend with a lambda function and reverses the lists as inputs" :out '((1 2 3) (3 2 1) (a b c) (c b a))) )