[erlang-questions] tail recursion : sharing a learning and a question

Witold Baryluk baryluk@REDACTED
Mon Aug 17 18:02:25 CEST 2009


Dnia 2009-08-16, nie o godzinie 18:44 +0530, Amit Murthy pisze:
> Hi,
> 
> I just learned that every iteration of the below code goes onto the stack.
> The evaluation of the "catch" has something to do with it though I could
> understand why.

It is not tail recursive version, because there is some additional work
after exit of last function, namely change catch handlers, to the
previous ones.


This should work.

do_maintenance(CacheSz, HKIntvl, Oldest) ->
    try
        do_maintance0(CacheSz, HKIntvl, Oldest);
    catch
        error:Error ->
            met_util:log("~p do_maintenance() caught exception ->
~p",[?MODULE, Error]),
            do_maintenance(CacheSz, HKIntvl, Oldest)
    end.

do_maintenance0(CacheSz, HKIntvl, Oldest) ->
        receive
            Message ->
                met_util:log_info(" pid -~p recieved unexpected
message[~p].",[self(), Message])

        after HKIntvl ->
            NOldest = do_cleanup(CacheSz, Oldest, 100),
            do_maintenance0(CacheSz, HKIntvl, NOldest)
        end.

-- 
Witold Baryluk <baryluk@REDACTED>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: To jest cz??? wiadomo?ci podpisana cyfrowo
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090817/878aa82c/attachment.bin>


More information about the erlang-questions mailing list