I'm a newbie when it comes to programming in lisp and ill be honest recursion is not my forte, I was tasked with writing a function that would simplify arithmetic equations but not solve them. Here is the guidelines this is a school project by the way.
*Write function simplify that takes any arithmetic expression as described above (not just the examples shown) and returns a new function in which the following improvements are made, if they are possible:
Multiplication sub-expression a. With a 0 as an argument, sub-expression is replaced by 0:
(* 3 5 0) -> 0b. With a 1 as an argument, the 1 is removed:(* 1 2 6) -> (* 2 6). If only one argument remains then the sub-expression is replaced by that argument:(* 1 6) -> 6Addition sub-expression a. Any occurrence of 0 is eliminated:
(+ 0 2 7 7) -> (+ 2 7 7). If only one argument remains, the sub-expression is eliminated:(+ 0 7) -> 7
My group mates and I have written this so far :
(defun simplify (lis)
(cond
((null lis) '())
((eq (car lis) '*)
(cond
((not(null (member '0 lis))) 0)
((not(null (member '1 lis))) (simplify (remove '1 lis)))
(t (simplify (cdr lis)))
)
)
((eq (car lis) '+)
(cond
(t 0)
)
)
((listp (car lis)) (cons (simplify (car lis))(simplify (cdr lis))))
(t (cons (simplify (car lis)) (simplify (cdr lis))))
)
)
We cant get it to work Correctly if you have any suggestions! Thank you also you can ignore our + function that isn't finished.
tcases, you need to iterate over all the arguments, replacing them with the result of callingsimplifyon them.mapcarwill be useful here.(* (+ 3 0) (* 1 10)), you need to callsimplifyon(+ 3 0)and(* 1 10), so that you get(* 3 10).