[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [plt-scheme] Defines in lets
From: pocm@rnl.ist.utl.pt (Paulo J. Matos)
Date: 18 May 2002 20:21:59 +0100
Hi all,
Why can't I do something like this:
> (let ((x 0)) (define (set-x y) (set! x y)) (define (read-x) x))
begin (possibly implicit): no expression after a sequence of internal definitions
I'm used to do it in Common Lisp like a way to avoid global
variables and use data encapsulation. How can I do it in Scheme?
In Scheme define only has local scope; if you change your example to
(let ((x 0))
(define (set-x y) (set! x y))
(define (read-x) x)
SOME-EXPRESSION-HERE)
then SOME-EXPRESSION-HERE can use set-x and read-x as well as x, but
nothing outside the let can use any of the three names. This
explains, then, why Scheme doesn't allow you to leave the
SOME-EXPRESSION-HERE out (as you did) and have a let where the body is
nothing but defines: there would be no point in it.
There is nothing in standardized Scheme that neatly corresponds to what
you are used to in Common Lisp. Various Scheme implementations have
non-standard extensions to the language that do what you want. If you
want to stay within the standardized language, the closest you can
come is:
(define set-x 'not-yet-set) ; or any other placeholder value
(define read-x 'not-yet-set)
(let ((x 0))
(set! set-x (lambda (y) (set! x y)))
(set! read-x (lambda () x)))
-Max Hailperin
Associate Professor of Computer Science
Gustavus Adolphus College
800 W. College Ave.
St. Peter, MN 56082
USA
http://www.gustavus.edu/~max/