Open oubiwann opened 9 years ago
Made some more progress on this tonight (see changes 7ea02cc73c15c6525b9591cda94e9b1c56f5dfcd and d2182255b833d33aad18c2201b9a0ad18adeee1a). The following now works:
(defmulti area)
(defmulti perim)
(defmethod area 'triangle
((`#m(base ,b height ,h))
(* b h (/ 1 2))))
(defmethod perim 'triangle
((`#m(base ,b height ,h))
(+ b h (math:sqrt (+ (* b b)
(* h h))))))
(defmethod area 'circle
((`#m(diameter ,d))
(area `#m(type circle radius ,(/ d 2))))
((`#m(radius ,r))
(* (math:pi) r r)))
(defmethod perim 'circle
((`#m(diameter ,d))
(perim `#m(type circle radius ,(/ d 2))))
((`#m(radius ,r))
(* 2 (math:pi) r)))
With usage being simply this:
> (area #m(type triangle base 6 height 10))
30.0
> (perim #m(type triangle base 6 height 10))
27.6619037896906
> (area #m(type circle diameter 8))
50.26548245743669
> (perim #m(type circle diameter 8))
25.132741228718345
So far, the macros only support the rather trivial usage shown above, with the type
hard-coded. The next steps are:
defmulti
Usage should be something like this:
A non-macro version of this could be done in the following manner:
These could then be used in the following manner:
Additional implementations could be added with ease:
Then use them:
See the following: