乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 89|回复: 8

[编程交流] [Help] Limit of recursive

[复制链接]

22

主题

326

帖子

185

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
243
发表于 2022-7-6 08:56:41 | 显示全部楼层 |阅读模式
Hi all ^^
I've tried to use recursive with a huge list, and find out that it has limit, but i don't know what exactly number ?
See in this Ex with LM:mAssoc :
  1. (defun LM:mAssoc ( key lst / pair )   (if (setq pair (assoc key lst))       (cons (cdr pair) (LM:mAssoc key (cdr (member pair lst))))   ))(defun test (num / lst)(setq i 0)(repeat num (setq lst (cons  (cons 1 (setq i (1+ i))) lst))))
Then test :
Please help me. Thank all ^^
回复

使用道具 举报

11

主题

968

帖子

919

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
99
发表于 2022-7-6 09:05:38 | 显示全部楼层
Not sure ... but it seems the largest value in your code which doesn't produce the error is 19996.
 
You could of course use an iterative MAssoc:
  1. ;; Multiple assoc, by Lee Mac(defun MAssoc (key lst / pair return) (while (setq pair (assoc key lst))   (setq return (cons (cdr pair) return)         lst    (cdr (member pair lst))   ) ) (reverse return))
Eg.:
  1. Command: (length (Massoc 1 (test 1000000)))1000000
Strangely I have another recursive MAlloc function:
  1. (defun mAssocR1 (key lst / res) (if (and lst (setq res (assoc key lst)))   (cons (cdr res) (mAssocR1 key (cdr (member res lst)))) ))
This gets a single value more before the error happens:
  1. Command: (length (mAssocR1 1 (test 19997)))19997Command: (length (mAssocR1 1 (test 19998)))Hard error occurred ***internal stack limit reached (simulated)
So I guess for large lists you'd be better off going with iterative routines. To check which you'd prefer, look at this thread: http://www.cadtutor.net/forum/showthread.php?55931-mAssoc-implementations
回复

使用道具 举报

26

主题

1495

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 09:18:08 | 显示全部楼层
Recursion limits used to be dependent on your hardware.  Maybe it still is.   -David
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 09:26:40 | 显示全部楼层
Recursion is always limited by the size of the stack memory it is allocated. When you run a program a 'stack' of activation records are maintained, there is one activation record for every active method - a method is 'active' if it has been called but hasn't yet returned, e.g.:
 
  1. (defun _copycat ( x )   (if (atom x)       x       (cons (_copycat (car x)) (_copycat (cdr x)))   ))
Here, the 'cons' method is still active when the recursive call is made. The activation record stores the parameters/variables and 'return address' for the method. The return address tells the interpreter where in the code to continue evaluation after the method returns.
 
When you call a method, such as 'cons' above, the activation record is 'pushed' onto the stack memory. When the method returns, the return address is read and the activation record is 'popped' from the stack memory.
 
However, now consider the case of recursion: the activation record for the 'cons' method remains on the stack when the recursive call is made; similarly, another activation record is pushed onto the stack when the next recursive call is made, and another, and another, until the recursion reaches the base case and the method returns - at which point the activation records are 'popped' from the stack at each level of the recursion.
 
Therefore the limiting factor is the size of the allocated stack memory, limiting how many activation records can be pushed onto the stack, and hence how many levels of recursion can be achieved.
回复

使用道具 举报

22

主题

326

帖子

185

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
243
发表于 2022-7-6 09:30:36 | 显示全部楼层
^^ Thanks all. From now on, i will try to avoid it. App can be slow, but can't crash ^^
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 09:36:10 | 显示全部楼层
 
On the contrary, recursion is usually far slower than iterative solutions.
 
Exaggerated example:
 

[code](defun fib_recurse ( n )   (if (
回复

使用道具 举报

22

主题

326

帖子

185

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
243
发表于 2022-7-6 09:45:34 | 显示全部楼层
So... what is the reason to use recursion somewhere if it Short, but slow and dificult to understand ? ^^
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 09:56:39 | 显示全部楼层
 
In some cases it results in concise and elegant solutions, and for others it is almost the obvious choice, such as when processing nested lists.
回复

使用道具 举报

22

主题

326

帖子

185

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
243
发表于 2022-7-6 10:00:45 | 显示全部楼层
Oh, right.. Thank you Lee. You help me like a "live dictionary" ^^
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-7 05:03 , Processed in 0.481737 second(s), 81 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表