请教关于表的问题。
有一个表'( (1 2)((3 4) (5 6)) (7 8) (((9 10) (11 12))(13 14)) )期望得到的表
'((1 2) (3 4) (5 6)(7 8)(9 10 )( 11 12) (13 14) )
或
(1 2 3 4 5 6 7 8 9 10 11 12 13 14 )
该怎么写.谢谢..
用循环或递归,遇到表就append
(defun flatten (lis / lis1 1st)
(while lis
(setq 1st(car lis))
(cond
((atom 1st)
(setq lis1 (cons 1st lis1)
lis(cdr lis)
)
)
(T (setq lis (append 1st (cdr lis))))
)
)
(reverse lis1)
)
Ref:
https://forums.autodesk.com/t5/v ... uzzle-2/td-p/893293 本帖最后由 自贡黄明儒 于 2018-8-8 08:39 编辑
(setq L '((1 2) ((3 4) (5 6)) (7 8) (((9 10) (11 12)) (13 14))))
(defun ww (L / A LST)
(while L
(setq a (car L))
(setq L (cdr L))
(if (= (type a) 'LIST)
(setq L (append a L))
(setq Lst (cons a Lst))
)
)
(reverse Lst)
)
第二种:
;;(WW '((1 2) ((3 4) (5 6)) (7 8) (((9 10) (11 12)) (13 14)))) => (1 2 3 4 5 6 7 8 9 10 11 12 13 14)
;;(WW '(1 2 3)) => (1 2 3)
(defun ww (L / A LST)
(if L
(progn
(setq a (car L))
(if (= (type a) 'LIST)
(append (ww a) (ww (cdr L)))
(cons a (ww (cdr L)))
)
)
)
)
本帖最后由 namezg 于 2018-8-8 09:59 编辑
;分解嵌套表(将多层表变为单层表)
;(zg-flat (setq lst '((0 1 2) ((3 4) (5 6 7)) (8 9 10 11) (((12 13 14 15 16) (17 18)) (19 20)))))
;(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
(defun zg-flat (lst)
(if lst
(append
(if (= (type (car lst)) 'LIST)
(zg-flat (car lst))
(list (car lst))
)
(zg-flat (cdr lst))
)
)
) XUEXIE学习学习 (defun ttt (lst)
(cond
((not lst) nil)
((atom (car lst)) (list lst))
((listp (caar lst)) (ttt (append (car Lst) (cdr lst))))
(t (cons (car lst) (ttt (cdr lst))))
)
) (defun listall(l)
(while(vl-some'listp l)
(setq l(apply'append(mapcar'(lambda(x)(if(listp x)x(list x)))l))))
l) 本帖最后由 namezg 于 2018-8-8 13:50 编辑
(defun zg-flat (lst / newlst)
(foreach x lst
(if(= (type x) 'LIST)
(setq newlst (append newlst (zg-flat x)))
(setq newlst (append newlst (list x)))
)
)
)
学习学习楼上多位大师的,谢谢
页:
[1]