4

I'm running through A brief introduction to Haskell and have come to the section on currying. (So, please correct any errors I've made in terminology.)

This section says:

Here is a two-argument function defined in our usual manner. Prelude> let myadd x y = x + y Prelude> myadd 3 4 7

And then a bit further on:

Here is a third equivalent way to define myadd, as an anonymous function returning another anonymous function. Prelude> let myadd = \x -> \y -> x + y Prelude> :t myadd myadd :: Integer -> Integer -> Integer

Even though the it says "equivalent", surprisingly (to a beginner like me) it's not, because the type of myadd is more general:

Prelude> :t myadd
myadd :: Num a => a -> a -> a
Prelude> myadd 1.5 2.5
4.0
Prelude> myadd3 1.5 2.5

<interactive>:12:8:
    No instance for (Fractional Integer) arising from the literal `1.5'
    Possible fix: add an instance declaration for (Fractional Integer)
    In the first argument of `myadd3', namely `1.5'
    In the expression: myadd3 1.5 2.5
    In an equation for `it': it = myadd3 1.5 2.5
Matt McHenry
  • 20,009
  • 8
  • 65
  • 64

0 Answers0