Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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)))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote