[Haskell-beginners] Num instnace for [Double]

Ben Gamari bgamari.foss
Thu Oct 3 13:38:36 UTC 2013


Nathan H?sken <nathan.huesken at posteo.de> writes:

> Hey
> Thanks for the answer one thing to discuss remains for me:
> With applicative I could add TimeSeries like this:
>
> print $ (+) <$> pure 4 <*> times
> (+) <$> times1 <*> times2
>
> ok .. but actually I find that (for this case) clumpsy.
> I would much prefer to write times1 + times2 (especially in complex cases):
>
> (times1 + times2)/(times3 * times4)
>
> Don't you agree? Or do I oversee something?
>
I admit that applicative notation isn't quite as convenient as working
with arithmetic expressions. As I've said in the past, overloading Num
isn't terribly desireable as you'll pretty much need to resign
yourself to partial implementations of some of the class members. A
middle path would be to instead define your own combinators for the
operations you need. For instance,

    %+% :: Num a => TimeSeries a -> TimeSeries a -> TimeSeries a
    %+% a b = (+) <$> a <*> b

    %*% :: Num a => TimeSeries a -> TimeSeries a -> TimeSeries a
    %*% a b = (*) <$> a <*> b

    ...

For at least some of these you could (ab)use the additive group classes
provided by vector-space[1] and linear[2] although there's no such class
for point-wise multiplication.

Cheers,

- Ben


[1] http://hackage.haskell.org/package/vector-space-0.7.1/docs/Data-AdditiveGroup.html
[2] http://hackage.haskell.org/package/linear-1.3/docs/Linear-Vector.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 489 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20131003/16458353/attachment.sig>



More information about the Beginners mailing list