Next: , Previous: , Up: slots   [Contents][Index]


4.2 Slots operations for pairs

When the block value is a Scheme pair, we could make a more descriptive use of the system operations with the following macros:

(define-syntax-rule ($car ?pair)
  (##sys#slot ?pair 0))

(define-syntax-rule ($cdr ?pair)
  (##sys#slot ?pair 1))

(define-syntax-rule ($car-set! ?pair ?new-value)
  (##sys#setslot ?pair 0 ?new-value))

(define-syntax-rule ($cdr-set! ?pair ?new-value)
  (##sys#setslot ?pair 1 ?new-value))

(define-syntax-rule ($car-set-immediate! ?pair ?new-value)
  (##sys#setislot ?pair 0 ?new-value))

(define-syntax-rule ($cdr-set-immediate! ?pair ?new-value)
  (##sys#setislot ?pair 1 ?new-value))

(define-syntax-rule ($pair-size ?pair)
  (##sys#size ?pair))

($pair-size '(a . b))           ⇒ 2

(let ((P (cons 'a 'b)))
  (values ($car P)
          ($cdr P)))
⇒ a b

(let ((P (cons 'a 'b)))
  ($car-set! P 'x)
  ($cdr-set! P 'y)
  (values ($car P)
          ($cdr P)))
⇒ x y

(let ((P (cons 'a 'b)))
  ($car-set-immediate! P 88)
  ($cdr-set-immediate! P 99)
  (values ($car P)
          ($cdr P)))
⇒ 88 99

Next: , Previous: , Up: slots   [Contents][Index]