previous | index | next

Mutable Pairs in Racket

While the official Scheme language specification includes set-car! and set-cdr!, the Racket implementation of Scheme does not.

Instead, Racket introduces a separate data type called a mutable pair whose constructor is mcons (as opposed to cons).

The selectors and mutators for mutable pairs are similarly named:

Operation Pair Mutable Pair
constructor cons mcons
selectors car mcar
cdr mcdr
mutators set-car! set-mcar!
set-cdr! set-mcdr!

An implementation of linked nodes in Racket:

     (define make-node mcons)
     (define node-element mcar)
     (define node-rest mcdr)
     (define node-set-element! set-mcar!)
     (define node-set-rest! set-mcdr!)

previous | index | next