Skip to content

Instantly share code, notes, and snippets.

@kinoshita-lab
Last active May 21, 2021 03:57
Show Gist options
  • Save kinoshita-lab/b76a55759a0d0968cd97 to your computer and use it in GitHub Desktop.
Save kinoshita-lab/b76a55759a0d0968cd97 to your computer and use it in GitHub Desktop.
put-coercion,get-coercion for SICP 2.5.2
(define coercion-list '())
(define (clear-coercion-list)
(set! coercion-list '()))
(define (put-coercion type1 type2 item)
(if (get-coercion type1 type2) coercion-list
(set! coercion-list
(cons (list type1 type2 item)
coercion-list))))
(define (get-coercion type1 type2)
(define (get-type1 listItem)
(car listItem))
(define (get-type2 listItem)
(cadr listItem))
(define (get-item listItem)
(caddr listItem))
(define (get-coercion-iter list type1 type2)
(if (null? list) #f
(let ((top (car list)))
(if (and (equal? type1 (get-type1 top))
(equal? type2 (get-type2 top))) (get-item top)
(get-coercion-iter (cdr list) type1 type2)))))
(get-coercion-iter coercion-list type1 type2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment