2015/11/01

SICP 問題 2.82

(define (apply-generic op . args)
  (let ((type-tags (map type-tag args)))
    (define (try-coercion args tags)
      (if (null? tags)
          (error "Nomethod for these types"
                 (list op type-tags))
          (let ((new-args (map (lambda (x) (get-coercion (car tags)
                                                         (type-tag x)))
                               args)))
            (let ((new-type-tags (map type-tag new-args)))
              (let ((peroc (get op (new-type-tags))))
                (if proc
                    (apply proc (contents new-args))
                    (try-coercion new-args (cdr tags))))))))
    (let ((pero (get op types)))
      (if proc
          (apply proc (map contents args))
          (try-coercion args typep-tags)))))

© 2022 wat-aro