Was ist mit den Typen in dieser Ghci-Session los?
Ich lerne Haskell und habe in Ghci rumgespielt, als ich auf etwas sehr Rätselhaftes gestoßen bin.
Erstellen Sie zunächst eine einfache Additionsfunktion:
Prelude> let add x y = x + y
Beachten Sie, dass es mit Ints und Floats funktioniert:
Prelude> add 3 4
7
Prelude> add 2.5 1.3
3.8
Erstellen Sie nun eine Apply-Funktion. Es ist identisch mit$
(aber nicht infixieren). Es funktioniert wie ein No-Op-Add:
Prelude> let apply f x = f x
Prelude> apply add 3 4
7
Prelude> apply add 2.5 1.3
3.8
Ok, jetzt machadd'
das ist das gleiche wieadd'
aber mitapply
:
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.
Hier sind die Typen:
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>
Warum tutadd'
haben einen anderen Typ alsapply add
?
Ist das eine merkwürdige Erscheinung, oder trifft das auf Haskell im Allgemeinen zu? (Und wie kann ich den Unterschied erkennen?)