[racket] section 11.5 from htdp

From: Horace Dynamite (horace.dynamite at gmail.com)
Date: Sat May 28 17:52:52 EDT 2011

On 28/05/2011 22:27, Krzysztof Adamczyk wrote:
> The template is easy:
>
> (define (exponent n x)
>    (cond [(zero? n) ...]
>             [else ... (sub1 n) ...]
>
> and this, along with the hint from the book, leads very quickly to
>
> (define (exponent n x)
>    (cond [(zero? n) 1]
>             [else (* (exponent (sub1 n) x) x)]))
>
> It works perfectly with inexact numbers, but when I change " * " to my
> multiply (which is very similar to the above exponent), it falls in an
> unending loop. Should I redefine my multiply function and write it for
> inexact numbers somehow? Or is the exponent wrong and my "is easy" and
> "leads very quickly" approach is not the right one?

Just to elaborate...

I think this might be an ambiguity in the text. The text is asking you 
to implement multiply function over the natural numbers (at least that's 
what I deduce), but there's a loose bound of "number" on the base of the 
exponent. So the result of an exponentiation can lie outside the set of 
naturals. The reason your multiply procedure doesn't work and '*' does 
is because '*' can deal with floating-point multiplication.

HTH,

Horace.


Posted on the users mailing list.