[Haskell-cafe] How to define an operation in terms of itself (but of different type)?

Luke Palmer lrpalmer at gmail.com
Sat Jan 24 10:59:29 EST 2009


2009/1/24 Olex P <hoknamahn at gmail.com>

> But you know it doesn't make too much sense because I also have to define
> addition Scalar + Vector (that means construct vector from scalar and add a
> vector), Vector + Scalar and so on. And as we are not able to overload
> operations in C++ like way we have to create several different operations
> even if their meaning is pretty close.


Well, yeah, but their meaning isn't *the same*, so we don't give them the
same name.

For vectors, putting a carat (or other signifier like a dot) on the side of
the operation which has the vector is relatively common practice.

Scalar +^ Vector
Vector ^+^ Vector

And so on.

And also, I wonder, what are you going and adding scalars to vectors for!?
(I've heard of multiplying scalars by vectors -- that's in the definition of
a vector space, but adding...?)

Oh, instead of overloading a million operations that just work
component-wise on vectors the way C++ guys do it, you can just define a
higher-order function:

vmap :: (Vector v) => (Double -> Double) -> v -> v

Or however it works out in your situation.  Then you can reserve those
precious symbols for things that are actually vectory, like inner products.

Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090124/e6adb0fc/attachment.htm


More information about the Haskell-Cafe mailing list