Question

(1)Create a LISP function FLATTEN, a function that returns all the elements of an arbitrarily nested...

(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.

Homework Answers

Answer #1
`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))))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT