i'm making basic modules 2d geometry , i've decided wrap coordinates in newtypes. can tell apart cartesian coordinates (newtype xy = xy(float,float)
) polar coordinates (newtype ra=ra(float,float)
), perhaps complex numbers; make them instances of num
, fractional
can overload operators.
but have defined either pair of float
s or pair of double
s? in trigonometric functions, double
s might work noticeably better in terms of precision; many of functions i'm writing xy
newtype i'd them generic possible. there way make newtype pair of numbers of fractional
class?
also, since find bit cumbersome write xy (0,0)
instead of (0,0)
, made ·
operator:
(·) b = xy (a,b)
but seems have precedence on rest, 3+4·2+1
evaluated 3+(xy (4,2))+1
. , neither function declarations, lambda expressions, etc... still have write \xy (a,b)->
.
thanks.
you can have type parameter in newtype:
newtype xy n = xy (n, n)
then can write functions work on pairs of in fractional
class:
foo :: fractional n => xy n -> xy n -> ...
you can use specific types xy double
if have operations make sense doubles.
unfortunately, can't reuse tuple syntax ((a, b)
), it's manageable if write type has 2 fields instead of wrapping pair:
data xy n = xy n n
now can write xy b
value or pattern. can make record convenient field syntax:
data xy n = xy { x, y :: n }
this works previous version also allows use x
, y
functions (ie x :: xy n -> n
) , in patterns.
Comments
Post a Comment