Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a lisp function that averages its three arguments, first checking to se

ID: 3865806 • Letter: 1

Question

1. Write a lisp function that averages its three arguments, first checking to see that they are numbers; if they are not, have the function AVERAGE return 'error.

2. Write a lisp function QUADRATIC of three arguments A, B, and C which returns the roots of the quadratic equation A z^2 - B z - C = 0 as a list. For example, (QUADRATIC 1 -2 3) would return (-3 1).

3. Modify the function QUADRATIC to the function QUADRATICL so that the new function returns the largest root.

4. The Euclidean norm of a vector (x1, x2 , x3, ..., xn) of length n is defined as sqrt((x1)^2 + (x2)^2 + ... + (xn)^2)). Suppose we represent a vector (x1, x2 , x3, ..., xn) as an n-element list (x1 x2 x3 ... xn). Write a function EUCLIDEAN_NORM that returns the norm of its input argument vector. Assume that the maximum length of the vector is 25.

5. If you are distressed that the names CAR and CDR are non-mnemonic, you are now in a position to do something about it. Define functions MYFIRST and MYREST that behave exactly like CAR and CDR, respectively.

6. Write a recursive lisp function COUNT-ATOMS that counts the number of non-nil atoms that appear at all levels of a list. For example, (COUNT-ATOMS '(a (b ((c))) (d (e) f))) returns 6.

7. Write a recursive function REPLACE_ALL that replaces all occurrences of an element from a list. For example, (REPLACE_ALL '(a x) '(a b (a) c (a d) e)) returns (x b (x) c (x d) e).

8. Write a lisp predicate NON-MEMBER that returns true if the value of its first argument does NOT occur in the value of its second. For example, (NON-MEMBER 'a '(b a c a)) should return false, while (NON-MEMBER 'a '(b c d)) should return true.

Explanation / Answer

Answer to Q1:
; a lisp function that averages its three arguments
(defun AVERAGE (n1 n2 n3)
   (if(and (typep n1 'integer) (typep n2 'integer) (typep n3 'integer))
       (/ ( + n1 n2 n3) 3)
       (write "error"))
)
; call the function to verify
(write(AVERAGE 10 20 30))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote