明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2242|回复: 8

如何让在表中插入子表?

[复制链接]
发表于 2009-4-7 07:42:00 | 显示全部楼层 |阅读模式
有没有一种函数,可以在lisp表单中某个位置插入一个子表啊,比如:'((A B)(C D)(E F)),我想在第二个子表后插入一个子表(1 2),应该如何实现呢,谢谢!
发表于 2009-4-7 09:21:00 | 显示全部楼层

;;;参数   n 位置

          e 插入的元素或表

          lst 表 

;;; 在n出插入元素或表

(defun insert-n (n e lst / a b)
  (setq a lst)
  (repeat n
    (setq b (cons (car a) b)
   a (cdr a)
    )
  )
  (setq a (cons e a))
  (foreach i b
    (setq a (cons i a))
  )
  a
)

;;;移除第n项

(defun remove-n (n lst / a b)
  (setq a lst)
  (repeat n
    (setq b (cons (car a) b)
   a (cdr a)
    )
  )
  (setq a (cdr a))
  (foreach i b
    (setq a (cons i a))
  )
  a
)

发表于 2009-4-7 09:24:00 | 显示全部楼层
给你贴一个函数吧
NBTF_Lst_Addnth表插入项(支持嵌套)
  1. ;;;_添加至表中的第index项后
  2. ;;index:'(0 5 3 1)表层
  3. ;;new:表或项 '(1 2 3 "d")或"f"
  4. ;;lst:可为多层表:'((a (()a()...)...)...)
  5. ;;(setq NBTV_AllFunsLIST (cons "NBTF_Lst_Addnth" NBTV_AllFunsLIST))
  6. (DEFUN NBTF_Lst_Addnth (index new lst / n re olditem newitem leftlst rightlst fun_substlst fun_sublst)
  7.   (defun fun_sublst (index_begin index_end lst / n re item)
  8.     (setq n 0)
  9.     (if (= -1 index_begin)
  10.       ;;(setq index_begin (length lst))
  11.       (setq index_begin 0)
  12.     )
  13.     (if (= -2 index_end)
  14.       (setq index_end (length lst))
  15.     )
  16.     (foreach item lst
  17.       (if (and (>= n index_begin) (<= n index_end))
  18.         (setq re (append re (list item)))
  19.       )
  20.       (setq n (1+ n))
  21.     )
  22.     re
  23.   )
  24.   (defun fun_substlst (index lst / n olditem newitem)
  25.     (setq n (car index))
  26.     (if (setq olditem (nth n lst))
  27.       (progn
  28.         (if (and(cdr index)(listp olditem))
  29.           (setq newitem (fun_substlst (cdr index) olditem)
  30.                 lst     (subst newitem olditem lst)
  31.           )
  32.           (setq leftlst  (fun_sublst 0 (1- n) lst)
  33.                 rightlst (fun_sublst n -2 lst)
  34.                 lst      (append leftlst (list new) rightlst)
  35.           )
  36.         )
  37.       )
  38.       (setq lst (append lst (list new)))
  39.     )
  40.     lst
  41.   )
  42.   ;;---------------
  43.   (setq n (car index))
  44.   (if (setq olditem (nth n lst))
  45.     (progn
  46.       (if (and(cdr index)(listp olditem))
  47.         (setq newitem (fun_substlst (cdr index) olditem)
  48.               lst     (subst newitem olditem lst)
  49.         )
  50.         (setq leftlst  (fun_sublst 0 (1- n) lst)
  51.               rightlst (fun_sublst n -2 lst)
  52.               lst      (append leftlst (list new) rightlst)
  53.         )
  54.       )
  55.     )
  56.     (setq lst (append lst (list new)))
  57.   )
  58.   lst
  59. )
  60. ;;_$ (NBTF_Lst_Addnth '(2) '(1 2) (list '("A" "B") '("C" "D") '("E" "F")))
  61. ;;(("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"))
 楼主| 发表于 2009-4-7 09:49:00 | 显示全部楼层

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

 楼主| 发表于 2009-4-8 14:06:00 | 显示全部楼层

经测试成功,在任意带有字表或元素的表中插入新表火或元素:

( defun insert_n (n e lst / a b) ;;n插入位置第n处后侧,即:1 2..n e n+1,亦即新插入子表位于第n+1处
 
        (setq a lst )
  
        (repeat n
  
                (setq b (cons (car a ) b)
  
                   a (cdr a)
    )

         )
  
  ( append (list (reverse b)) (list  e) (list a)) ;返回插入后的新表
)

发表于 2011-3-22 14:24:25 | 显示全部楼层
非常好,经过测试可用,我把最后一句改了一下
( append (reverse b)   e    a)
这样更好一些。
发表于 2012-6-8 12:14:18 | 显示全部楼层
值得学习,沉贴多年,才被翻出,可惜啊。
发表于 2012-11-7 11:31:01 | 显示全部楼层
高人大作!!受教了
发表于 2012-11-7 13:36:45 | 显示全部楼层
超强!学习细节!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-9-26 03:50 , Processed in 0.189601 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表