如何让在表中插入子表?
有没有一种函数,可以在lisp表单中某个位置插入一个子表啊,比如:'((A B)(C D)(E F)),我想在第二个子表后插入一个子表(1 2),应该如何实现呢,谢谢! <p>;;;参数 n 位置</p><p> e 插入的元素或表</p><p> lst 表 </p><p>;;; 在n出插入元素或表</p><p>(defun insert-n (n e lst / a b)<br/> (setq a lst)<br/> (repeat n<br/> (setq b (cons (car a) b)<br/> a (cdr a)<br/> )<br/> )<br/> (setq a (cons e a))<br/> (foreach i b<br/> (setq a (cons i a))<br/> )<br/> a<br/>)</p><p></p><p>;;;移除第n项</p><p>(defun remove-n (n lst / a b)<br/> (setq a lst)<br/> (repeat n<br/> (setq b (cons (car a) b)<br/> a (cdr a)<br/> )<br/> )<br/> (setq a (cdr a))<br/> (foreach i b<br/> (setq a (cons i a))<br/> )<br/> a<br/>)</p><p></p> 给你贴一个函数吧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"))
<p>谢谢,我好啊好哦研究一下,我用lisp要处理很多数据,然后根据数据在CAD绘图,发现lisp的数据处理比较麻烦!发现楼上两位大侠比较牛,呵呵!</p> <p>经测试成功,在任意带有字表或元素的表中插入新表火或元素:</p><p>( defun insert_n (n e lst / a b) ;;n插入位置第n处后侧,即:1 2..n e n+1,亦即新插入子表位于第n+1处<br/> <br/> (setq a lst )<br/> <br/> (repeat n<br/> <br/> (setq b (cons (car a ) b)<br/> <br/> a (cdr a) <br/> )</p><p> )<br/> <br/> ( append (list (reverse b)) (list e) (list a)) ;返回插入后的新表<br/>)</p> 非常好,经过测试可用,我把最后一句改了一下
( append (reverse b) e a)
这样更好一些。 值得学习,沉贴多年,才被翻出,可惜啊。 高人大作!!受教了 超强!学习细节!
页:
[1]