[plt-scheme] What's wrong with define inside begin which is inside if

From: Robby Findler (robby at cs.uchicago.edu)
Date: Mon Sep 4 11:28:14 EDT 2006

At Mon, 04 Sep 2006 10:21:23 -0400, Andre Mayers wrote:
> If I try to evaluate the following expression
>  
>     (if (< 1 0)
>         (begin (define z 4)
>                (* z 2))
>         3)
>  
> I got the following answer 
>  
>     define: not allowed in an expression context in: (define z 4)
>  
>  
> What is the principle behind that ? 

I don't know that there is a principle behind it (you'll want to talk
to the authors of the r5 report (or possibly comp.lang.scheme) for that
one!) but if you replace the begin with a `let' (that binds no
variables) you can use define internally, eg:

  (if (< 1 0)
      (let ()
        (define z 4)
        (* z 2))
      3)

hth,
Robby



Posted on the users mailing list.