[racket] [realm of racket] contract error with 'smaller' function

From: Spencer Gordon (sgord512 at gmail.com)
Date: Sun Oct 20 12:07:44 EDT 2013

guess is a procedure, and not a number.  

The error message is telling you that it expects a number and not a procedure as the argument to sub1.

If you evaluate guess in the REPL, you can see the problem:

> guess
#<procedure:guess>

What you want is the following:

> (guess)
50


The problem was that you were passing in the procedure guess to sub1, when what you wanted to pass in was the result of applying the procedure. When you have 0 argument functions, this distinction can sometimes be confusing, since the difference between 

guess 

and 

(guess) 

can seem purely stylistic. But the former is a procedure, while the latter is the resulting of evaluating that procedure with no arguments. In Racket, the use of parens around an identifier means that the identifier is a procedure that is being applied with 0 arguments.

I hope that helps.

-Spencer Gordon


On Sunday, October 20, 2013 at 11:42 AM, Joe Python wrote:

> #lang racket
> (define lower 1)
> (define upper 100)
> 
> (define (guess)
>   (quotient (+ lower upper) 2))
> 
> (define (smaller)
>   (set! upper (max lower (guess (sub1 guess))))
>   (guess)) 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131020/50c568ad/attachment.html>

Posted on the users mailing list.