[racket] set! racket behaviour

From: Dan Grossman (djg at cs.washington.edu)
Date: Mon Oct 31 16:25:15 EDT 2011

I also found it counterintuitive that

#lang racket
(define x 0)
(set! x 1)

works, but moving the set! out of the module and into the REPL does
not.  But Dave has pointed to the exact line in the Guide that makes
this clear: A module-level definition is mutable only if there is a
set! for it in the module.  Once you know that "reasonable compromise"
[as I put it in my emerging lecture materials], everything works as
expected.

--Dan

On Mon, Oct 31, 2011 at 10:20 AM, David Vanderson
<david.vanderson at gmail.com> wrote:
> I think you are running into the issue described here:
> http://docs.racket-lang.org/guide/module-set.html
>
> "Along the same lines, when a module contains no set! of a particular
> identifier that is defined within the module, then the identifier is
> considered a constant that cannot be changed—not even by re-declaring the
> module."
>
> Try in your definitions window:
> #lang racket
> (define counter 0)
> (set! counter 0)
>
>
> Thanks,
> Dave
>
> On 10/31/2011 09:58 AM, jkaczorek at aol.pl wrote:
>
> Hi,
>
> I’m using DrRacket.
> In an edit window I have written:
>
> #lang racket
> (define counter 0)
>
> I’m pressing “Run” button and next, in a command line, after running a
> command:
>
> (set! counter (add1 counter))
>
> I receive a message “set!: cannot modify a constant: counter”
>
> The problem can be resolved by changing the language (Language/Choose
> language/R5RS and unchecking “Disallow redefinition of initial binding”) but
> I suppose that in this way I can lose an access to the racket libraries.
> Of course, I realize, that redefinition of the variables is not good
> (functional) programming style but sometimes it’s necessary. Is there a
> possibility of such modifications without changing language?
>
> Jerzy
>
>
>
> _________________________________________________
>   For list-related administrative tasks:
>   http://lists.racket-lang.org/listinfo/users
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
>



Posted on the users mailing list.