The code in the question doesn't work because in this line:
(set! list (cons value (cdr list)))
... you're assigning a new value to the list
parameter, but that's a change local to the procedure, it won't be reflected "outside" the procedure in the p
variable. You're simply pointing the list
parameter to a different list, but p
is still pointing to the original list once the procedure returns.
In fact, set-car!
has to be a primitive operation. But fear not, in Racket there is a set-mcar!
and a bunch of other operations for mutable pairs, if unbound you just have to import mpair
:
(require scheme/mpair)
And consistently use all of the mutable pair operations. For instance, here's how to build a mutable list:
(define mlst (mcons 1 (mcons 2 (mcons 3 '()))))
Or alternatively:
(define mlst (mlist 1 2 3))
To access elements:
(mcar mlst)
=> 1
(mcdr mlst)
=> (mcons 2 (mcons 3 '()))
And of course, to mutate elements:
(set-mcar! mlst 0)
mlst
=> (mcons 0 (mcons 2 (mcons 3 '())))
(set-mcdr! mlst '())
mlst
=> (mcons 0 '())