michaelmmacleod
2020-2-1 08:04:40

@slack1 Not sure if this helps, but > (define p (cons 2 2)) > (print-graph #t) > (list p p) '(#0=(2 . 2) #0#) shows that the list of two p doesn’t allocate more than one (cons 2 2). You can even mutate it with set-*! type functions if p is bound to a mutable object, like an mcons > (define p (mcons 2 2)) > p (mcons 2 2) > (define g (list p p)) > (print-graph #t) > g (list #0=(mcons 2 2) #0#) > (set-mcar! p 3) > g (list #0=(mcons 3 2) #0#)


philip.mcgrath
2020-2-2 06:21:20

@slack1 It gets quite technical and you certainly don’t need to understand all of it, but you might find the Racket Reference chapter on the evaluation model helpful, especially the distinction between “values,” “objects,” and “locations”: https://docs.racket-lang.org/reference/eval-model.html#%28part._.Objects_and_.Imperative_.Update%29 In Racket, “a value is a reference to an object [potentially] stored somewhere else. For example, a value can refer to a particular vector that currently holds the value 10 in its first slot. If an object is modified via one value, then the modification is visible through all the values that reference the object.” Some other languages use these words with different meanings. For example, Swift conflates the mutability of data structures with the assignability of variable/constant identifiers, which I find annoying.