Tuesday, April 24, 2012

Implementing car and cdr in Scheme


;;;;#########Implimenting Set-car and set cdr######;;;;;
(define (cons! x y)
 (define (set-x! v) (set! x v))
 (define (set-y! v) (set! y v))
 (define (dispatch m)
  (cond   ((eq? m 'car) x)
   ((eq? m 'cdr) y)
   ((eq? m 'set-car) set-x!)
   ((eq? m 'set-cdr) set-y!)
   (else 'error)
  )

 )
 dispatch
)

(define (car! z)
 (z 'car)
)

(define (cdr! z)
 (z 'cdr)
)

(define (set-car z v)
 ((z 'set-car) v)
)


(define (set-cdr z v)
 ((z 'set-cdr) v)
)

No comments:

Post a Comment