joshua.e.cottrell
2020-5-21 02:58:12

I have a boolean variable that I’m initializing like so (define *chain-welded* #f). Later I do (set! **chain-welded** #t) but I get this error: set!: assignment disallowed; cannot modify a constant constant: *chain-welded* I’m guessing chain-welded is “becoming” the constant #f and cannot be reassigned. How would I track a boolean value if that’s the case? I may be asking the wrong question, if so, I can post my code (99 lines) to http://pasterack.org\|pasterack.org.


samth
2020-5-21 03:08:18

@joshua.e.cottrell you can’t use set! on a definition from a different module


samth
2020-5-21 03:09:01

if you need to let other people change *chain-welded*, I would either make it a box or provide a function set-chain-welded! from that module that does the set!.


joshua.e.cottrell
2020-5-21 03:15:11

Ok, maybe I’m unclear on what comprises a module. I’m just using one file in Dr Racket. Line 20 defines *chain-welded* and later in a procedure I try to change it. Are those considered two different modules?


notjack
2020-5-21 03:16:15

Does the top of your file start with #lang racket or #lang racket/base?


joshua.e.cottrell
2020-5-21 03:18:32

#lang racket


samth
2020-5-21 03:18:55

Maybe posting your code is the best option


joshua.e.cottrell
2020-5-21 03:20:53

joshua.e.cottrell
2020-5-21 03:22:50

Is the problem that I’m “playing” the game in the interactions area of Dr Racket when the definition sets chain-welded?


samth
2020-5-21 03:23:24

Yes, that’s the problem


notjack
2020-5-21 03:23:57

the module is the code in the definitions area, repl code is not part of the module


samth
2020-5-21 03:24:01

The set! is actually inside a macro, so the compiler thinks that defintion is immutable until you end up changing later in a macro expansion.


samth
2020-5-21 03:24:12

and that expansion happens in the interactions window


samth
2020-5-21 03:24:19

so it isn’t in the module


joshua.e.cottrell
2020-5-21 03:26:11

Ah, very helpful clarification - the macro expanding in the interactions window


samth
2020-5-21 03:26:27

This ends up being a little confusing in exactly the case you’ve run into. What you probably want is to define a function that changes chain-welded and expand to calling that function.


joshua.e.cottrell
2020-5-21 03:27:01

Do I fix that by providing the procedures that can be called from the interactions window?


notjack
2020-5-21 03:27:13

(oh I see what happened now, nevermind my earlier comment)


joshua.e.cottrell
2020-5-21 03:28:55

Or I could compile it and play it at the terminal?


samth
2020-5-21 03:30:01

No, you don’t have to provide them. Add a function (define (set-chain-welded! v) (set! *chain-welded* v)) and change line 85 to use it.


samth
2020-5-21 03:30:35

You probably have to do something similar for *bucket-filled*


joshua.e.cottrell
2020-5-21 03:31:53

Perfect! And it makes sense. Thanks for the explanation too.


samth
2020-5-21 03:32:56

:slightly_smiling_face:


joshua.e.cottrell
2020-5-21 03:36:00

And I’m back in the same situation if I create a macro to build both my set-chain-welded! and set-bucket-filled! procedures, right?


samth
2020-5-21 03:36:35

No, that’s fine as long as you use the macro inside the module


samth
2020-5-21 03:37:09

The rule is “all set!s must be inside the module that defines the name, and you can’t use macros to cheat”


joshua.e.cottrell
2020-5-21 03:39:54

Very good @samth, maybe I’ll try that after I get the first solution ironed out. Thanks again!