[racket] begin vs +

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Aug 2 08:10:43 EDT 2010

At Mon, 02 Aug 2010 20:42:34 +0900 (JST), Keiko Nakata wrote:
> This code prints "hi" twice,
> 
> (define d (box #f))
> 
> (+ (begin (let/cc k (begin (set-box! d k) 3))) (begin (print "hi") 9)) 
> 
> ((unbox d) 0)
> 
> whereas this prints "hi" once
> 
> (define d (box #f))
> 
> (begin (begin (let/cc k (begin (set-box! d k) 3))) (print "hi")) 
> 
> ((unbox d) 0)
> 
> Why?

Each form at the top level is wrapped in a prompt, and the content of a
top-level `begin' is spliced into the top level.

The latter program is the same as

 (define d (box #f))

 (let/cc k (begin (set-box! d k) 3))
 (print "hi")

 ((unbox d) 0)

where there's an implicit prompt around the `let/cc' form.



Posted on the users mailing list.