黄翔 发表于 2018-8-7 20:41:33

请教关于表的问题。

有一个表'( (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 )
该怎么写.谢谢..

自贡黄明儒 发表于 2018-8-7 20:58:10

用循环或递归,遇到表就append

Andyhon 发表于 2018-8-7 22:51:02


(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:13:27

本帖最后由 自贡黄明儒 于 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:58:09

本帖最后由 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))
    )
)
)

mapple666 发表于 2018-8-8 10:44:27

XUEXIE学习学习

nzl1116 发表于 2018-8-8 12:36:36

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

llsheng_73 发表于 2018-8-8 12:48:15

(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:34:45

本帖最后由 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)))
                )
      )
)

evayleung 发表于 2018-8-9 00:53:17

学习学习楼上多位大师的,谢谢
页: [1]
查看完整版本: 请教关于表的问题。