Skip to content

general

(define (type-tag datum)
    (if (pair? datum)
        (car datum)
        (error "Bad tagged datum: TYPE-TAG" datum)))
(define (contents datum)
    (if (pair? datum)
        (cdr datum)
        (error "Bad tagged datum: CONTENTS" datum)))
(define (attach-tag type-tag contents)
    (cons type-tag contents))
(define *op-table* (make-hash-table))
(define (put op type proc)
    (hash-table/put! *op-table* (list op type) proc))
(define (get op type)
    (hash-table/get *op-table* (list op type) #f))
(define (apply-generic op . args)
    (let ((type-tags (map type-tag args)))
        (let ((proc (get op type-tags))) 
            (if proc
                (apply proc (map contents args))
                (error
                    "No method for these types: APPLY-GENERIC"
                    (list op type-tags))))))
(define (real-part z) 
    (apply-generic 'real-part z)) 
(define (imag-part z) 
    (apply-generic 'imag-part z)) 
(define (magnitude z) 
    (apply-generic 'magnitude z)) 
(define (angle z) 
    (apply-generic 'angle z))