I guess trampoline allows to emulate tail call optimization on
mutually recursive functions, that is functions that, without
trampoline and in an environment that would do TCO automatically, have
a tail call to the other function, and the other function also has a
tail call to another function ... with only tail calls to functions
having tail calls.

Your code does not have this property : the function you have written,
as implemented, are not mutually recursive on the tail call.

Regards,

-- 
Laurent


2009/6/10 Shawn Hoover <shawn.hoo...@gmail.com>:
>
> On Tue, Jun 9, 2009 at 6:04 PM, arasoft <t...@arasoft.de> wrote:
>>
>> I tried this:
>>
>> (declare F)
>>
>> (defn M [n]
>>  (if (zero? n)
>>    0
>>    (- n (F (M (dec n))))))
>>
>> (defn F [n]
>>  (if (zero? n)
>>    1
>>    (- n (M (F (dec n))))))
>>
>> and for large n I got the expected stack overflow. Then I tried to
>> trampoline the functions:
>>
>> (declare F)
>>
>> (defn M [n]
>>  (if (zero? n)
>>    0
>>    #(- n (F (M (dec n))))))
>>
>> (defn F [n]
>>  (if (zero? n)
>>    1
>>    #(- n (M (F (dec n))))))
>>
>> Now (trampoline #(M 7)) yields
>> java.lang.ClassCastException: user$M__760$fn__762 cannot be cast to
>> java.lang.Number (NO_SOURCE_FILE:0)
>>
>> Does anybody have an idea to get this to work?
>
> I don't know how to make it work, but I can tell you why you're getting a
> ClassCastException. In the definition of M, you have (F (M (dec n)). This is
> returned in a fn, which is fine until it gets called. When called, the inner
> M could return a fn, which gets passed to F, which assumes it's a number and
> tries to use it in a subtraction operation.
>
> Shawn
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to