Write a LISP function(up lst) that removes a pair of parentheses from each top-l
ID: 669900 • Letter: W
Question
Write a LISP function(up lst) that removes a pair of parentheses from each top-level element of lst like in the following example. If a top-level element is not a list, it is included in the output as is.
> (up '(1 2 3 4))
(1 2 3 4)
> (up '((1) 2 3 (4)))
(1 2 3 4)
> (up '((1) (2) (3) (4)))
(1 2 3 4)
> (up '((1 2) (3 4)))
(1 2 3 4)
> (up '((1 (2)) 3))
(1 (2) 3)
> (up '(((1 2 3 4))))
((1 2 3 4))
> (up '((1 2) (((3 4))) 5))
(1 2 ((3 4)) 5)
Explanation / Answer
(defn up [lst]
(if (empty? lst)
()
(if (list? (first lst))
(up (first lst))
(up (concat (first lst) (rest lst)))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.