[Haskell-beginners] Arrow vs. function
Michael Orlitzky
michael at orlitzky.com
Mon Mar 23 23:53:25 UTC 2015
On 03/23/2015 04:43 PM, martin wrote:
> Hello all
>
> I skimmed several Arrow tutorial and looked at the pretty diagrams and thought to myself "Hmm, these guys seem to take
> input and produce output".
>
> But that's what a function does. So what's the fundamental difference between an arrow and a fuction?
They're basically functions, and you won't steer yourself wrong thinking
about them like that.
By analogy, in Haskell, if you wanted to add two floating point numbers
you would need to define a floating point addition:
floatAdd :: Float -> Float -> Float
Then if you wanted to add two integers, you'd need a second function:
intAdd :: Int -> Int -> Int
The prelude already abstracts this away for you -- both Int and Float
are instances of the Num class, which gives you,
(+) :: (Num a) => a -> a -> a
So at that point, you might ask yourself, what's the point of a Num?
Aren't they just (floating point, int, etc.) numbers? The answer is
"yes," and you don't want to have twenty different addition functions
for everything that is some kind of number, so it makes sense to group
all number-things into one typeclass.
Likewise, all function-things are grouped into the Arrow typeclass so
that you can compose them without worrying about the exact type.
Everything in the Arrow typeclass is some kind of function, and you can
compose them with the Arrow class methods.
More information about the Beginners
mailing list