Wednesday, March 25, 2009

collect-numbers function

I stumbled on this page and started to solve the problems, Here is solution to the ex5.

Define (collect-numbers s-exp) to return a list of all the numbers in the s-expression s-exp. s-exp may be an atom, a list, or a list of s-expressions.

> (collect-numbers 1)
(1)
> (collect-numbers 'a)
NIL
> (collect-numbers '(1 (b (2 c) ((3)))))
(1 2 3)
Solution:
(defun collect-numbers (s-exp)
(if (atom s-exp)
(if (numberp s-exp) (list s-exp) nil)
(mapcan #'collect-numbers s-exp)))

No comments:

Post a Comment