On Wed, Sep 23, 2015 at 11:40 PM, Alexis King <lexi.lam...@gmail.com> wrote:

>
> Er, no you can’t... `begin` doesn’t create an internal definition context
> (or even a new scope). You can use an empty `let` instead:
>
> (define (f x)
>   (if (even? x)
>       (/ x 2)
>       (let ()
>         (define a (* x 2))
>         (define b (* x 3))
>         (f (+ a b)))))
>
> Or you can use `block` from racket/block.


I can verify that the version with begin gives a "define: not allowed in an
expression context in: (define a (* x 2))" error.

The let version works. Thanks for showing me `block` (
http://docs.racket-lang.org/reference/block.html?q=block#%28form._%28%28lib._racket%2Fblock..rkt%29._block%29%29
).

"Supports a mixture of expressions and mutually recursive definitions, as
in a module body. Unlike an internal-definition context, the last
defn-or-expr need not be an expression."

#lang racket
(require racket/block)

(define (f x)
  (if (even? x)
      (/ x 2)
      (block
        (define a (* x 2))
        (define b (* x 3))
        (f (+ a b)))))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to