(1)Create a LISP function FLATTEN, a function that returns all
the elements of an arbitrarily nested list in a single-level
list.
(FLATTEN ’((A B (R)) A C (A D ((A (B)) R) A))) should return (A B R
A C A D A B R A).
(2) Create a Lisp function EXP-EVAL, a function that evaluates an arithmetic expression. You may assume that the binary operators used for an arithmetic expression are: +, -, *, and /, and each of the (nested) expression is well-formed (parenthesized) binary expression recursively. You may assume: each list is recursively well-formed with binary operator only (infix notation).
For example, (EXP-EVAL '(2 + (3 * 5))) should return 17.
`push` changes the symbol binding in scope. Thus the recursion `(rflatten el acc)` has it's own `acc` which is the result there but you don't do anything with the returned result and it doesn't alter the callee `acc`. Perhaps a `(setf acc (rflatten el acc))` would fix that: (defun flatten (lst) (labels ((rflatten (lst1 acc) (dolist (el lst1) (if (listp el) (setf acc (rflatten el acc)) (push el acc))) acc)) (reverse (rflatten lst nil))))
Get Answers For Free
Most questions answered within 1 hours.