Yeah that’s why I used ...
, I only knew about syntax-case
Didn’t think to change afterwards tho, thanks for pointing that out!
I thought your original code was, “one \n, followed by zero or more \n”. Which together actually means “1 or more” — not “2 or more”? Anyway, now you know the available building blocks, you can figure it out. :smile:
What is the rule of thumb for deep vs shallow copy? For e.g. I find the following confusing > (define (set-var! x)
(set! x 5))
> (define (set-table! h)
(dict-set! h 1 'a))
> (define x 2)
> (set-var! x)
> x
2 ; Value didn't change, deep copy
> (define h (make-hash))
> (set-table! h)
> h
'#hash((1 . a)) ; Value changed, shallow copy
That’s not about shallow vs deep copy (which both are a kind of object mutation). Rather, it’s about binding mutation vs object mutation.
it’s about your set-var!
function, which is busted
in (define (set-var! x) (set! x 5))
you’re only setting the function variable x
, which is completely unrelated to any variables that happen to be around where you call set-var!
otherwise how would (set-var! (+ 1 2 3))
work?
if you want to pass mutable storage locations to functions like that, you should use boxes
> (define (set-var! x)
(set-box! x 5))
> (define x (box 2))
> (set-var! x)
> (unbox x)
5
where can I read up more about binding mutation vs object mutation? Specifically which procedure does what
set!
is the only form that does binding mutation, and it’s not a procedure. Procedures can’t mutate bindings. They can only mutate objects.
Ah yes, my bad. It slipped my mind that it’s a special form. Thanks @notjack!