I want to write code (using recursive function) to unnest the parentheses in a LIST in Lisp language. Example:
(unnest '(1 (2 (3)) (4 5))) ==> (1 2 3 4 5)
Thanks. Any help is appreciated!
I want to write code (using recursive function) to unnest the parentheses in a LIST in Lisp language. Example:
(unnest '(1 (2 (3)) (4 5))) ==> (1 2 3 4 5)
Thanks. Any help is appreciated!
(defun unnest (lst)
(cond ((null? lst) '())
((not (list? lst)) (list lst))
(t
(append (unnest (car lst))
(unnest (cdr lst))))))
> (unnest '(1 (2 (3)) (4 5)))
(1 2 3 4 5)
Basically the idea is as follows:
Hope it helps.