I'm learning Haskell, and I was playing around in ghci when I came across something very puzzling.
First, create a simple add function:
Prelude> let add x y = x + y
Note that it works with ints and floats:
Prelude> add 3 4
7
Prelude> add 2.5 1.3
3.8
Now create an apply function. It's identical to $
(but not infix). It works like a no-op on add:
Prelude> let apply f x = f x
Prelude> apply add 3 4
7
Prelude> apply add 2.5 1.3
3.8
Ok, now make add'
which is the same as add'
but using apply
:
Prelude> let add' = apply add
Prelude> add' 3 4
7
Prelude> add' 2.5 1.3
<interactive>:1:9:
No instance for (Fractional Integer)
arising from the literal `1.3' at <interactive>:1:9-11
Possible fix: add an instance declaration for (Fractional Integer)
In the second argument of `add'', namely `1.3'
In the expression: add' 2.5 1.3
In the definition of `it': it = add' 2.5 1.3
Wat.
Here are the types:
Prelude> :t add
add :: (Num a) => a -> a -> a
Prelude> :t apply add
apply add :: (Num t) => t -> t -> t
Prelude> :t add'
add' :: Integer -> Integer -> Integer
Prelude>
Why does add'
have a different type than apply add
?
Is this a ghci oddity, or is this true in Haskell in general? (And how can I tell the difference?)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…