[mercury-users] Quick Recursion Question
Ian MacLarty
maclarty at csse.unimelb.edu.au
Sat Apr 21 09:48:25 AEST 2012
Hi Charles,
On Sat, Apr 21, 2012 at 7:42 AM, Charles Shuller
<charles.shuller at gmail.com> wrote:
> I was reading some sample code in the Posix extension library and saw:
>
> main_loop(Data, ListenFd, !IO) :-
> await_conn(ListenFd, ConnFd, !IO),
> handle_conn(Data, ConnFd, !IO),
> main_loop(Data, ListenFd, !IO).
>
>
> And the first thing I think of is that the stack will be consumed without
> bounds as every recursion into main_loop consumes more memory.
>
> Then I thought that perhaps Mercury predicates didn't work quite the same
> way as a function in C.
>
> So will the above simply consume ever more ram as every request calls
> main_loop again?
>
No, for tail recursive calls (i.e. where the recursive call is last)
the same stack slot will be reused.
Ian.
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to: mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions: mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the users
mailing list