Exercise 2.82

(define (apply-generic op . args)
  (let ((type-tags (map type-tag args)))
    (let ((proc (get op ‘type-tags)))
      (if proc
          (proc (map contents args))
          (try-coercion op . args)))))
(define (try-coercion op . args)
  (let ((n (length args)))
    (define (iter count)
      (if (= count n)
          (error “No proc defined for type-tags”)
          (let ((convert-to (stream-ref count args)))
              (let ((current-type (type-tags convert-to)))
                (if (convert-all args current-type)
                    (apply (get op (repeat n ‘current-type)) (convert-all args current-type))
                    (iter (+ 1 count)))))))
    (iter 0))) 

(define (convert-all args current-type)
  (if (null? args)
      ‘()
      (cons ((get-coercion (type-tags (car args)) current-type) (car args)) (convert-all (cdr args) current-type))))

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: