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

Olex P hoknamahn at gmail.com
Sat Jan 24 11:33:34 EST 2009


Concerning Scalar + Vector i.e. +^ it's just a shortcut. Very useful even if
it's not good (?) from the design point of view. Also one could find useful
shortcut like .<, .<=, .>, .>= which mean compare length of vector. It's not
usual for vector algebra but probably still make sense.

What I want to ask you guys can we define a function with arbitrary number
of parameters? Actually not really arbitrary but just several possibilities
(as we have with value constructors).
For instance cross product can have 1, 2 or 3 vectors as inputs depends on
the dimension. Is it 2d, 3d or 4d case.


On Sat, Jan 24, 2009 at 3:59 PM, Luke Palmer <lrpalmer at gmail.com> wrote:

> 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/51a41665/attachment.htm


More information about the Haskell-Cafe mailing list