Chapter 2, Building Abstractions with Data

Section - 2.4 Multiple Representations for Abstract Data

Exercise 2.75


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#lang sicp

(#%require (only racket/base error))

(define (make-from-mag-ang r a) 
   (define (dispatch op) 
         (cond ((eq? op 'real-part) (* r (cos a)))
               ((eq? op 'imag-part) (* r (sin a))) 
               ((eq? op 'magnitude) r) 
               ((eq? op 'angle) a) 
               (else (error "Invalid operation" op))
         )
   ) 
   dispatch
)