Anonymous user
Angles (geometric), normalization and conversion: Difference between revisions
Angles (geometric), normalization and conversion (view source)
Revision as of 01:56, 1 November 2021
, 2 years ago→{{header|Haskell}}: minor details
m (→{{header|Haskell}}: minor details) |
m (→{{header|Haskell}}: minor details) |
||
Line 1,440:
The solution of this seemingly trivial task could be elegantly done by type classes. Each angle unit is represented as a distinct type, preventing from implicit combination of different units. Moreover, adding new units doesn't imply writing new transformers or normalizers.
Isomorphims between all angular types are defined via representation
<lang haskell>{-# LANGUAGE RankNTypes #-}
Line 1,485:
instance Show Rad where
show (Rad r) = printf "
instance Angle Rad where
Line 1,509:
instance Show Grad where
show (Grad g) = printf "%.3gg"
instance Angle Grad where
Line 1,549:
λ> angle pi :: Rad
λ> angle 1000 :: Gon
Line 1,561:
λ> (from :: Gon -> Rad) 100
λ> (from :: Rad -> Gon) $ (pi/2)
Line 1,620:
<pre>λ> main
converting to radians
[
[
[
[
[
converting to degrees
|