-
Notifications
You must be signed in to change notification settings - Fork 0
/
getput.rkt
50 lines (40 loc) · 1.48 KB
/
getput.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#lang racket
(provide put get put-coercion get-coercion)
(define global-array '())
(define (make-entry k v) (list k v))
(define (key entry) (car entry))
(define (value entry) (cadr entry))
(define (put op type item)
(define (put-helper k array)
(cond ((null? array) (list(make-entry k item)))
((equal? (key (car array)) k) array)
(else (cons (car array) (put-helper k (cdr array))))))
(set! global-array (put-helper (list op type) global-array)))
(define (get op type)
(define (get-helper k array)
(cond ((null? array) #f)
((equal? (key (car array)) k) (value (car array)))
(else (get-helper k (cdr array)))))
(get-helper (list op type) global-array))
(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))