Hi Randall,

yes, I am very aware that it is not an efficient solution ;-)

It is only an exercise just to make (Scheme) students aware of the
possibilities of mutual definitions.
The assignment in Kent's book reads:
;;; Exercise 2.8.6.
;;; All of the recursive procedures shown so far have been directly
;;; recursive. That is, each procedure directly applies itself to a new
;;; argument. It is also possible to write two procedures that use each
;;; other, resulting in indirect recursion. Define the procedures odd?
;;; and even?, each in terms of the other. [Hint: What should each return
;;; when its argument is 0?]

Thanks for the reply!
   Arie


2008/12/5 Randall R Schulz <[EMAIL PROTECTED]>

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