Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any limit to recursion in lisp?

I enjoy using recursion whenever I can, it seems like a much more natural way to loop over something then actual loops. I was wondering if there is any limit to recursion in lisp? Like there is in python where it freaks out after like 1000 loops? Could you use it for say, a game loop?

Testing it out now, simple counting recursive function. Now at >7000000!

Thanks alot

like image 712
Isaiah Avatar asked Jun 08 '10 01:06

Isaiah


People also ask

Is there a limit to recursion?

Due to this, the recursion limit of python is usually set to a small value (approx, 10^4). This means that when you provide a large input to the recursive function, you will get an error. This is done to avoid a stack overflow. The Python interpreter limits the recursion limit so that infinite recursions are avoided.

Does Lisp support recursion?

In Lisp, however, recursion is not only well supported, but it is fundamental to the design and spirit of the language. This is perhaps one main reason why the language is hard to grasp.

What is the maximum depth of recursion?

The maximum recursion depth in Python is 1000. You can change the limit by calling sys. setrecursionlimit() method. Consider this a dangerous action!

What is the maximum number of levels in a recursion?

19) What is the maximum number of levels in a Recursion? Explanation: There is no limit.


1 Answers

Scheme mandates tail call optimization, and some CL implementations offer it as well. However, CL does not mandate it.

Note that for tail call optimization to work, you need to make sure that you don't have to return. E.g. a naive implementation of Fibonacci where there is a need to return and add to another recursive call, will not be tail call optimized and as a result you will run out of stack space.

like image 60
Sid Heroor Avatar answered Sep 22 '22 16:09

Sid Heroor