-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex2-81.scm
28 lines (24 loc) · 1.21 KB
/
ex2-81.scm
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
#lang racket/load
(load "data-directed-programming/setup.scm")
(define (same? items item)
(if (null? items)
#t
(and (eq? (car items) item) (same? (cdr items) item))))
(define (present? item)
(not (null? item)))
(define (apply-generic op . args)
(let ((type-tags (map get-tag args)))
(let ((proc (get op type-tags)))
(if (not (null? proc))
(apply proc (map contents args))
(if (and (>= (length args) 2) (not (same? (type-tags) (car type-tags))))
(let ((type1 (car type-tags))
(type2 (cadr type-tags))
(a1 (car args))
(a2 (cadr args)))
(let ((t1->t2 (get-coercion type1 type2))
(t2->t1 (get-coercion type2 type1)))
(cond ((present? t1->t2) (apply-generic op (t1->t2 a1) a2))
((present? t2->t2) (apply-generic op a1 (t2->t1 a2)))
(else (error "Action dispatch error! Types not compatible")))))
(error "Action Dispatch error method not defined!"))))))