1. (5 points) Which of the following are LISP atoms? ATOM atom 17 atom (0) - NIL atom (A.B) - 2. (3 points) Convert the following S-expressions into dot notation (A) (a . nil ) ((B)) ( (b . nil) . nil ) ( ( ) ( ) ) ( nil . ( nil . nil )) 3. (4 points) What is the value of each of the following expressions? (1 + (* 4 5) ) error, or 21 if you assumed (1+ (* 4 5) ) (car (quote (a list) ) ) a (car '(a list) ) a (cdr '(a list) ) (list) 4. (4 points) What is the result of evaluating the following? (setq x '(eval '(setq x 'eval) ) ) x is set to (EVAL '(SETQ X 'EVAL)) 5. (14 points) Copy the following LISP program and run it for different values of x. ;;; EXAMPLE1A.LSP (defun EXAMPLE1 ( ) (let ((x nil) ) (loop (cond (( null x) (format t "Enter Number:" ) (setq x (read) ) ) (( > x 39) (format t "Too Big:" ) (setq x nil ) ) (( > x 9) (prin1 'x ) (setq x (- x 10) ) ) (( = x 9) (prin1 'ix ) (setq x 0 ) ) (( > x 4) (prin1 'v ) (setq x (- x 5) ) ) (( = x 4) (prin1 'iv ) (setq x 0 ) ) (( > x 0) (prin1 'i ) (setq x (1- x) ) ) ((zerop x ) (setq x nil ) ) (terpri) ) ) ) ) x should be integer. x<0 error x=0 nothing x>0 < 40 roman numbers are printed x>=40 too big 6. (10 points) Define a function that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. (defun squares ( x y z ) (let ( (res (+ (* x x) (* y y) (* z z) )) ) (if (< x y) (if (< x z) (- res (* x x) ) (- res (* z z )) ) (if (< y z) (- res (* y y) ) (- res (* z z )) ) ) ) ) 7. (10 points) The built-in Common Lisp function abs (absolute value) could be defined as follows: (defun abs (x) (if (< x 0) (- x) x)) Write a new version of the absolute-value function, called new-abs, that produces equivalent values but uses “or” and “and” instead of “if” or “cond.” (defun my-abs(x) (or (and (< x 0) (- x)) x )) 8. (10 points) In the spirit of rewriting, consider the proposed new implementation of the “if” special form, in terms of “cond”. (defun new-if (predicate then-clause else-clause) (cond (predicate then-clause) (t else-clause))) The following examples show new-if working as intended. ? (new-if nil 'then 'else) ELSE ? (new-if t 'then 'else) THEN Construct an example for which new-if does not produce the same results as if. Explain why it is not possible to define if as a function in terms of cond. ? (setq x 4) 4 ? (if nil (setq x 2) x) 4 ? (new-if nil (setq x 2) x) 2 Functions' arguments are evaluated before they are passed to the function. The arguments may have side effects, i.e. assignments, which doesn't let the function work as intended. 9. (10 points) Write a function fringe that takes a list as argument and returns a list whose elements are all the atoms appearing in the original list or any of its sublists, arranged in left-to-right order. For example, ? (setf x (cons (list 1 2) (list 3 4))) ((1 2) 3 4) ? (fringe x) (1 2 3 4) ? (fringe (list x x)) (1 2 3 4 1 2 3 4) (defun fringe (x) (if (null x) nil (if (atom x) (list x) (apply 'append (mapcar 'fringe x))))) 10.(10 points) The Common Lisp function mapcar takes as arguments a function and a list, and returns a new list whose elements are the results of applying the function to each element of the original list. Write your own implementation, called new-mapcar, that produces the same result without using any of the built-in mapping functions. (defun new-mapcar (f l) (if (null l) nil (cons (funcall f (car l)) (new-mapcar f (cdr l)) ) ) )