To sort out atoms first and then sublists from a list in LISP

后端 未结 5 989
滥情空心
滥情空心 2020-12-07 04:15

I have this homework in LISP where I need to sort out atoms and then sublists from a list. I\'m sure this is supposed to be easy task but as I\'m not much of a programmer th

5条回答
  •  忘掉有多难
    2020-12-07 04:20

    You can do this recursive way:

    (defun f (lst) 
        (cond 
            ((null lst) nil)
            ((atom (car lst)) 
            (append (list (car lst)) (f (cdr lst)))) 
            (T
                (append (f (cdr lst)) (list (f (car lst))))
            )
        )
    )
    (step (f '(5 -1 (2 6 1) (8 7 -3) (0 (9 4)) -6)))
    

    Output:

    step 1 --> (F '(5 -1 (2 6 1) (8 7 -3) ...))                                                                   
    step 1 ==> value: (5 -1 -6 (0 (9 4)) (8 7 -3) (2 6 1))
    

提交回复
热议问题