R6RS:翻訳:Standard Libraries:17 Mutable pairs

R6RS:翻訳:Standard Libraries:17 Mutable pairs

17 章 変更可能な対

(rnrs mutable-pairs (6)) ライブラリで提供される手続きにより、以前割り当てた対の car と cdr フィールドに新しい値を代入することができるようになる。

[procedure] (set-car! pair obj)

objpair の car フィールドに格納する。 set-car!手続きは未定義値を返す。

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

変更不可能な対が set-car! に渡されると、 &assertion コンディション型の例外が発生する。

[procedure] (set-cdr! pair obj)

objpair の cdr フィールドに格納する。 set-cdr!手続きは未定義値を返す。

変更不可能な対が set-cdr! に渡されると、 &assertion コンディション型の例外が発生する。

(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)
More ...