enjoysj 发表于 2009-4-7 07:42:00

如何让在表中插入子表?

有没有一种函数,可以在lisp表单中某个位置插入一个子表啊,比如:'((A B)(C D)(E F)),我想在第二个子表后插入一个子表(1 2),应该如何实现呢,谢谢!

highflybir 发表于 2009-4-7 09:21:00

<p>;;;参数&nbsp;&nbsp; n 位置</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e 插入的元素或表</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lst 表&nbsp;</p><p>;;; 在n出插入元素或表</p><p>(defun insert-n&nbsp;(n e lst / a b)<br/>&nbsp; (setq a lst)<br/>&nbsp; (repeat n<br/>&nbsp;&nbsp;&nbsp; (setq b (cons (car a) b)<br/>&nbsp;&nbsp; a (cdr a)<br/>&nbsp;&nbsp;&nbsp; )<br/>&nbsp; )<br/>&nbsp; (setq a (cons e a))<br/>&nbsp; (foreach i b<br/>&nbsp;&nbsp;&nbsp; (setq a (cons i a))<br/>&nbsp; )<br/>&nbsp; a<br/>)</p><p></p><p>;;;移除第n项</p><p>(defun remove-n&nbsp;(n lst / a b)<br/>&nbsp; (setq a lst)<br/>&nbsp; (repeat n<br/>&nbsp;&nbsp;&nbsp; (setq b (cons (car a) b)<br/>&nbsp;&nbsp; a (cdr a)<br/>&nbsp;&nbsp;&nbsp; )<br/>&nbsp; )<br/>&nbsp; (setq a (cdr a))<br/>&nbsp; (foreach i b<br/>&nbsp;&nbsp;&nbsp; (setq a (cons i a))<br/>&nbsp; )<br/>&nbsp; a<br/>)</p><p></p>

jxphklibin 发表于 2009-4-7 09:24:00

给你贴一个函数吧
NBTF_Lst_Addnth表插入项(支持嵌套)

;;;_添加至表中的第index项后
;;index:'(0 5 3 1)表层
;;new:表或项 '(1 2 3 "d")或"f"
;;lst:可为多层表:'((a (()a()...)...)...)
;;(setq NBTV_AllFunsLIST (cons "NBTF_Lst_Addnth" NBTV_AllFunsLIST))
(DEFUN NBTF_Lst_Addnth (index new lst / n re olditem newitem leftlst rightlst fun_substlst fun_sublst)
(defun fun_sublst (index_begin index_end lst / n re item)
    (setq n 0)
    (if (= -1 index_begin)
      ;;(setq index_begin (length lst))
      (setq index_begin 0)
    )
    (if (= -2 index_end)
      (setq index_end (length lst))
    )
    (foreach item lst
      (if (and (>= n index_begin) (<= n index_end))
      (setq re (append re (list item)))
      )
      (setq n (1+ n))
    )
    re
)
(defun fun_substlst (index lst / n olditem newitem)
    (setq n (car index))
    (if (setq olditem (nth n lst))
      (progn
      (if (and(cdr index)(listp olditem))
          (setq newitem (fun_substlst (cdr index) olditem)
                lst   (subst newitem olditem lst)
          )
          (setq leftlst(fun_sublst 0 (1- n) lst)
                rightlst (fun_sublst n -2 lst)
                lst      (append leftlst (list new) rightlst)
          )
      )
      )
      (setq lst (append lst (list new)))
    )
    lst
)
;;---------------
(setq n (car index))
(if (setq olditem (nth n lst))
    (progn
      (if (and(cdr index)(listp olditem))
      (setq newitem (fun_substlst (cdr index) olditem)
            lst   (subst newitem olditem lst)
      )
      (setq leftlst(fun_sublst 0 (1- n) lst)
            rightlst (fun_sublst n -2 lst)
            lst      (append leftlst (list new) rightlst)
      )
      )
    )
    (setq lst (append lst (list new)))
)
lst
)
;;_$ (NBTF_Lst_Addnth '(2) '(1 2) (list '("A" "B") '("C" "D") '("E" "F")))
;;(("A" "B") ("C" "D") (1 2) ("E" "F"))

_$ (setq l1 '((6 8)5 6 ("a" "c"("第三层" "adf"))))
((6 8) 5 6 ("a" "c" ("第三层" "adf")))
_$ (NBTF_Lst_Addnth '(0 1) "a" l1)
((6 "a" 8) 5 6 ("a" "c" ("第三层" "adf")))
_$ (NBTF_Lst_Addnth '(0) "a" l1)
("a" (6 8) 5 6 ("a" "c" ("第三层" "adf")))
_$ (NBTF_Lst_Addnth '(1) "a" l1)
((6 8) "a" 5 6 ("a" "c" ("第三层" "adf")))
_$ (NBTF_Lst_Addnth '(3 2 1) "a" l1)
((6 8) 5 6 ("a" "c" ("第三层" "a" "adf")))

_$ (NBTF_Lst_Addnth '(0 1) '(1 2) (list '("A" "B") '("C" "D") '("E" "F")))
(("A" (1 2) "B") ("C" "D") ("E" "F"))

_$ (NBTF_Lst_Addnth '(0 2) '(1 2) (list '("A" "B") '("C" "D") '("E" "F")))
(("A" "B" (1 2)) ("C" "D") ("E" "F"))

_$ (NBTF_Lst_Addnth '(1 1) '(1 2) (list '("A" "B") '("C" "D") '("E" "F")))
(("A" "B") ("C" (1 2) "D") ("E" "F"))

_$ (NBTF_Lst_Addnth '(0 0) '(1 2) (list '("A" "B") '("C" "D") '("E" "F")))
(((1 2) "A" "B") ("C" "D") ("E" "F"))

_$ (NBTF_Lst_Addnth '(1) '(1 2) (list '("A" "B") '("C" "D") '("E" "F")))
(("A" "B") (1 2) ("C" "D") ("E" "F"))

_$ (NBTF_Lst_Addnth '(2) '(1 2) (list '("A" "B") '("C" "D") '("E" "F")))
(("A" "B") ("C" "D") (1 2) ("E" "F"))

enjoysj 发表于 2009-4-7 09:49:00

<p>谢谢,我好啊好哦研究一下,我用lisp要处理很多数据,然后根据数据在CAD绘图,发现lisp的数据处理比较麻烦!发现楼上两位大侠比较牛,呵呵!</p>

enjoysj 发表于 2009-4-8 14:06:00

<p>经测试成功,在任意带有字表或元素的表中插入新表火或元素:</p><p>( defun insert_n (n e lst / a b) ;;n插入位置第n处后侧,即:1 2..n e n+1,亦即新插入子表位于第n+1处<br/>&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq a lst )<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (repeat n<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq b (cons (car a ) b)<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a (cdr a) <br/>&nbsp;&nbsp;&nbsp;&nbsp;)</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;( append (list (reverse b)) (list&nbsp; e) (list a)) ;返回插入后的新表<br/>)</p>

daanff 发表于 2011-3-22 14:24:25

非常好,经过测试可用,我把最后一句改了一下
( append (reverse b)   e    a)
这样更好一些。

newbuser 发表于 2012-6-8 12:14:18

值得学习,沉贴多年,才被翻出,可惜啊。

zyhandw 发表于 2012-11-7 11:31:01

高人大作!!受教了

cable2004 发表于 2012-11-7 13:36:45

超强!学习细节!
页: [1]
查看完整版本: 如何让在表中插入子表?