On Friday 05 December 2008 08:07, Arie van Wingerden wrote:
> Hi,
>
> I tried to use definitions which call each other, like so:
>
> (defn is-odd? [n]
>       (if (= n 0)
>           false
>           (is-even? (- n 1))))
>
> (defn is-even? [n]
>       (if (= n 0)
>           true
>           (is-odd? (- n 1))))
>
> but obviously Clojure rejects this, saying:
>    Exception in thread "main" java.lang.Exception: Unable to resolve
> symbol: is-even? in this context
> which refers to "is-even?" in the definition of is-odd?
>
> Since I am very new to Clojure I may have missed a language construct
> to resolve this issue :-)

You can (def is-even?) before your (defn is-odd? ...), which will allow 
it to compile.

However, this implementation is very inefficient, since it' will call 
mutually recursively back and forth (without any kind of tail-call 
optimization, at least as written) decrementing the initial argument 
until it gets to zero.

The trampoline mechanism can fix the fact that this code consumes an 
amount of stack space linearly dependent on the initial argument, but 
clearly this is not the way to test for evenness or oddness!


> Thx,
>    Arie


Randall Schulz

--~--~---------~--~----~------------~-------~--~----~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to