明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1356|回复: 2

[求助]子函数能否调用自身来进行循环?

[复制链接]
发表于 2008-7-17 20:30:00 | 显示全部楼层 |阅读模式

[求助]子函数能否调用自身来进行循环?

 

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2008-7-17 21:30:00 | 显示全部楼层

前辈的一些讨论
Ref: http://autocad.xarch.at/stdlib/archive/10/msg00054.html
  1. ;;; by Randy Richardson
  2. (defun subst1 (index new lst / counter lst1)
  3.   (setq counter 0)
  4.   (repeat index
  5.     (setq lst1 (cons (nth counter lst) lst1))
  6.     (setq counter (1+ counter))
  7.   )
  8.   (setq lst1 (cons new lst1))
  9.   (setq counter (1+ counter))
  10.   (repeat (- (length lst) (length lst1))
  11.     (setq lst1 (cons (nth counter lst) lst1))
  12.     (setq counter (1+ counter))
  13.   )
  14.   (reverse lst1)
  15. )
  16. ;;; by Frank Oquendo
  17. (defun replace-element (index newelement lst / tmp)
  18.   (repeat index
  19.     (setq tmp (cons (car lst) tmp)
  20.    lst (cdr lst)
  21.     )
  22.   )
  23.   (append (reverse tmp) (list newelement) (cdr lst))
  24. )
  25. ;;; by Tony Tanzillo
  26. (defun SetNth (index newitem lst / i)
  27.    (setq i -1)
  28.    (mapcar
  29.      '(lambda (elem)
  30.          (if (eq index (setq i (1+ i)))
  31.             newitem
  32.             elem
  33.          )
  34.       )
  35.       lst
  36.    )
  37. )
  38. There's a number of posts here offering solutions, but
  39. if you need to perform multiple replacements of elements
  40. in the same list, by position, then none of the solutions
  41. you've seen so far, including the one Reini posts a link
  42. to (with a password), are very effective.
  43. If you have to do many replacements, you should
  44. index your list, by cons'ing each element with a
  45. unique integer value. This allows you to use (SUBST)
  46. to quickly replace elements:
  47. (defun index-list (lst / i)
  48.    (setq i -1)
  49.    (mapcar '(lambda (elem) (cons (setq i (1+ i)) elem)) lst)
  50. )
  51. Given the list (A B C D E F), the above function
  52. returns the list:
  53.     ((0 . A) (1 . B) (2 . C) (3 . D) ...)
  54. With this resulting list, you can now use (subst)
  55. to replace only one element by position. This is
  56. much more effecient if you have many replacements
  57. to perform on the same list.
  58. To replace an element at a given position:
  59.    (setq indexed-list
  60.       (subst (cons <index> <newvalue>)
  61.              (nth <index> indexed-list)
  62.              indexed-list
  63.       )
  64.    )
  65. You can still quickly reference elements in the list
  66. by position, using (cdr (nth <index> <indexed-list>))
  67. Or, once you're finished replacing elements, you can
  68. just do (mapcar 'cdr <indexed-list>) to remove the
  69. indexing.
  70. ;;; by Tony Tanzillo ===============================
  71. ;;; by Reini Urban
  72. (defun std-%setnth (new i lst / fst len)
  73.   (cond
  74.     ((minusp i) lst)
  75.     ((> i (setq len (length lst))) lst)
  76.     ((> i (/ len 2))
  77.       (reverse (std-%setnth new (1- (- len i)) (reverse lst))))
  78.     (T
  79.       (append
  80.         (progn
  81.           (setq fst nil)        ; ; possible VL lsa compiler bug
  82.           (repeat (rem i 4)
  83.             (setq fst (cons (car lst) fst)
  84.                   lst (cdr lst)))
  85.           (repeat (/ i 4)
  86.             (setq fst (cons (cadddr lst)
  87.                             (cons (caddr lst)
  88.                                   (cons (cadr lst)
  89.                                         (cons (car lst) fst))))
  90.                   lst (cddddr lst)))
  91.           (reverse fst)
  92.         )
  93.         (if (listp new) new (list new)) ; v0.4001
  94.         (cdr lst)))))
  95. ;|
  96. ;;; old slower versions:
  97. (defun STD-RPLACE (lst i new)
  98.   (if (< -1 i (length lst))     ; fixed v0.3003
  99.     (append (std-firstn i lst)
  100.             (list new)
  101.             (std-nthcdr (1+ i) lst))
  102.     lst))
  103. |;
  104. ;;; by Marc'Antonio Alessi
  105. ;;; newitem /= nil
  106. (defun ALE_SUBST_NTH (index newitem lst / len r_lst olditem )
  107.   (if (and newitem (> (setq len (- (length lst) (1+ index))) -1))
  108.     (progn
  109.       (setq
  110.         r_lst   (reverse lst)
  111.         olditem (nth index lst)
  112.       )
  113.       (while (/= index (length (setq r_lst (cdr (member olditem r_lst))))))
  114.       (while (/= len   (length (setq lst   (cdr (member olditem   lst))))))
  115.       (append (reverse r_lst)  (list newitem) lst)
  116.     )
  117.     lst
  118.   )
  119. )
发表于 2008-7-17 21:49:00 | 显示全部楼层
函数自身调用就是“递归”。只要有限次的“递归”(确保堆栈不溢出),且函数结束条件设定合理就行。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-9-21 04:12 , Processed in 0.124704 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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