Mutable pairs

The procedures provided by the (r6rs mutable-pairs)library allow new values to be assigned to the car and cdr fields of previously allocated pairs.

16.1  Procedures

procedure:  (set-car! pair obj) 

Stores obj in the car field of pair. Returns the unspecified value.

(define (f) (list ’not-a-constant-list))
(define (g) ’(constant-list))
(set-car! (f) 3)                     ===⇒  
(set-car! (g) 3)                     ===⇒  unspecified
; should raise  &assertion exception

If an immutable pair is passed to set-car!, an exception with condition type &assertion should be raised.

procedure:  (set-cdr! pair obj) 

Stores obj in the cdr field of pair. Returns the unspecified value.

If an immutable pair is passed to set-cdr!, an exception with condition type &assertion should be raised.

(let ((x (list ’a ’b ’c ’a))
      (y (list ’a ’b ’c ’a ’b ’c ’a)))
  (set-cdr! (list-tail x 2) x)
  (set-cdr! (list-tail y 5) y)
  (list
   (equal? x x)
   (equal? x y)
   (equal? (list x y ’a) (list y x ’b)))) 
                ===⇒  (#t #t #f)