polar.scm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (define-module (polar)
  2. #:use-module (rnrs base)
  3. #:use-module ((guile) #:select (lambda* λ simple-format))
  4. #:use-module (tagged-data)
  5. #:use-module (math)
  6. #:export (make-from-real-imag
  7. make-from-mag-ang
  8. rectangular?
  9. data-tag
  10. real-part
  11. imag-part
  12. magnitude
  13. angle
  14. install-package))
  15. (define data-tag 'polar)
  16. ;;; CONSTRUCTORS
  17. (define make-from-real-imag
  18. (λ (real imag)
  19. (attach-tag data-tag
  20. (cons (sqrt (+ (square real)
  21. (square imag)))
  22. (atan imag real)))))
  23. (define make-from-mag-ang
  24. (λ (mag ang)
  25. (attach-tag data-tag
  26. (cons mag ang))))
  27. ;;; ACCESSORS
  28. (define magnitude
  29. (λ (num)
  30. (car num)))
  31. (define angle
  32. (λ (num)
  33. (cdr num)))
  34. (define real-part
  35. (λ (num)
  36. (simple-format #t "real-part in polar: ~a\n" num)
  37. (* (magnitude num)
  38. (cos (angle num)))))
  39. (define imag-part
  40. (λ (num)
  41. (* (magnitude num)
  42. (sin (angle num)))))
  43. (define polar?
  44. (λ (datum)
  45. (eq? (type-tag datum) data-tag)))
  46. ;; But actually ... it is currently the same for all packages! But
  47. ;; that would require the main module to know about all procedures
  48. ;; defined in each package or prescribe a common interface without
  49. ;; package specific additions. Package specific additiona would become
  50. ;; useless, if install package was generic. -- So lets keep it in the
  51. ;; package for now.
  52. (define install-package
  53. (λ (lookup-table put)
  54. (let iter
  55. ([funcs° (list make-from-real-imag
  56. make-from-mag-ang
  57. rectangular?
  58. data-tag
  59. real-part
  60. imag-part
  61. magnitude
  62. angle)])
  63. (cond
  64. [(null? funcs°) lookup-table]
  65. [else
  66. (put lookup-table op type func)
  67. (iter (cdr funcs°))]))))