[racket] On (void) and interactions

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Thu May 26 10:59:41 EDT 2011

On May 26, 2011, at 10:08 AM, Robby Findler wrote:

> The #lang racket void is not the same thing as the void that comes out
> of (say) intermediate's assignments and definitions. I'm not sure what
> the intended behavior is but if you supply a small, complete example
> that would probably help someone figure out if you're seeing a bug in
> a printer or just the expected behavior.

These are both pretty short.

Here's the program in which "(void)" is NOT printed:

; In Definitions...
(define-struct my-posn (x y swapper formatter))

; build-with-swapper-and-formatter : number number -> my-posn
(define (build-with-swapper-and-formatter xval yval)
  (local [(define p1
            (make-my-posn xval yval
                          (lambda ()
                            (local ((define temp (my-posn-x p1)))
                              (begin (set-my-posn-x! p1 (my-posn-y p1))
                                     (set-my-posn-y! p1 temp))))
                          (lambda ()
                            (format "(~a, ~a)" (my-posn-x p1) (my-posn-y p1)))))]

; In Interactions...
(define here (build-with-swapper-and-formatter 3 4))
((my-posn-formatter here))
; prints "(3, 4)"
((my-posn-swapper here))
; doesn't print anything
((my-posn-formatter here))
; prints "(4, 3)"

And here's the one in which it IS printed:

; In Definitions...
; make-my-posn : number number -> (symbol -> number-or-string-or-nothing)
(define (make-my-posn xval yval)
  (local [(define stored-x xval)
          (define stored-y yval)]
    (lambda (message)
      (cond [(symbol=? message 'x)
            [(symbol=? message 'y)
            [(symbol=? message 'swap)
             (local [(define temp stored-x)]
               (begin (set! stored-x stored-y)
                      (set! stored-y temp)))]
            [(symbol=? message 'format)
             (format "(~a, ~a)" stored-x stored-y)]
             (error "Unrecognized message")]))))

; In Interactions...
(define here (make-my-posn 3 4))
(here 'format)
; prints "(3, 4)"
(here 'swap)
; prints (void)
(here 'format)
; prints "(4, 3)"

Both are in Advanced Student Language, DrRacket

Stephen Bloch
sbloch at adelphi.edu

Posted on the users mailing list.