Flatten Nests Function in Lisp - need help understanding

前端 未结 3 1721
名媛妹妹
名媛妹妹 2020-12-06 23:29

I\'ve been trying to find a way to condense nested lists into numbers that go back in the original list, but I having some trouble.

I\'ve been looking at the flatte

3条回答
  •  不知归路
    2020-12-06 23:55

    (defun flatten (l)
    

    Above defines a function FLATTEN with one argument called L.

      (cond
    

    IF

        ((null l) nil)
    

    the value of the argument L is the empty list, return the empty list.

        ((atom l) (list l))
    

    or if the argument L is an atom (i.e. not a list), then return a list with the atom as its only item.

        (t 
    

    or else we have a non-empty list

           (loop for a in l
    

    then loop through all the items in the list which is the value of L

            appending (flatten a)
    

    and append the results of flattening each list item.

    ))))
    

提交回复
热议问题