You're right, it fails when the call to recur should dispatch to another 
method.

But as long as you are calling the same implementation it does seem to work:

*(ns multi-recur)*
*
*
*(defmulti tail-call #(first %&))*
*
*
*(defmethod tail-call :recur [x n]*
*  (if (zero? n)*
*    (println x "Made it to" n)*
*    (recur x (dec n))))*
*    *
*(defmethod tail-call :overflow [x n]*
*  (if (zero? n)*
*    (println x "Made it to" n)*
*    (tail-call x (dec n))))*
*  *
*(tail-call :recur 5000)*
*(tail-call :overflow 5000)*

I don't know any work arounds for the situation where there dispatch 
resolves to another method implementation.

Juan

On Monday, December 17, 2012 2:08:39 PM UTC-3, Jonas wrote:
>
> recur doesn't work well with multimethods:
>
>     (defmulti foo identity)
>  
>     (defmethod foo 1 [n]
>       (recur (dec n)))
>  
>     (defmethod foo 0 [n]
>       :ok)
>  
>     (foo 1) ;; runs forever
>
> Jonas
>
> On Monday, December 17, 2012 6:56:34 PM UTC+2, juan.facorro wrote:
>>
>> What about recur <http://clojure.org/special_forms#recur>? 
>>
>> It's a special form used for tail call optimizations.
>>
>> Juan
>>
>> On Monday, December 17, 2012 1:32:31 PM UTC-3, bruce li wrote:
>>>
>>> Hello, everyone.
>>>
>>> I'm currently trying to model an automata using multi-method. So in my 
>>> code, I have something like:
>>>
>>> (defmethod trans :state
>>>      [state]
>>>      ; .......
>>>      (trans ......))))
>>>
>>> In the last line, the trans will be called with some different state.
>>>
>>> However, it seems that such call still consumes stack and I quickly come 
>>> to a stack overflow error when the states are reached multiple times.
>>>
>>> I'm wondering if there is some ways to optimize the code.
>>>
>>> Thanks!
>>>
>>

-- 
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