Ich versuche, den Funktionsanwendungsoperator ($
) in Haskell zu verstehen.
Ich arbeite die Beispiele in "Learn You a Haskell" durch und dachte, ich hätte das folgende Beispiel verstanden:
Prelude> map ($ 3) [(+4), (*10), (^2), sqrt]
[7.0,30.0,9.0,1.7320508075688772]
Dann habe ich die folgende Variante versucht, die auch gut funktioniert hat:
Prelude> map ($ 3) [(+4), (*10), (\x -> x^2), sqrt]
[7.0,30.0,9.0,1.7320508075688772]
Zuletzt habe ich versucht, die dritte Funktion in der Liste wie folgt zu ändern, was einen Fehler erzeugt:
Prelude> map ($ 3) [(+4), (*10), (\x -> 2^x), sqrt]
:53:38:
Ambiguous type variable `b0' in the constraints:
(Floating b0)
arising from a use of `sqrt' at :53:38-41
(Integral b0) arising from a use of `^' at :53:33
(Num b0) arising from the literal `3' at :53:8
Probable fix: add a type signature that fixes these type variable(s)
In the expression: sqrt
In the second argument of `map', namely
`[(+ 4), (* 10), (\ x -> 2 ^ x), sqrt]'
In the expression: map ($ 3) [(+ 4), (* 10), (\ x -> 2 ^ x), sqrt]
Prelude>
Es scheint so, als ob die letzte sqrt
Funktion irgendwie mit dem vorherigen Listenelement verbunden ist, da die folgende Variante gut funktioniert:
Prelude> map ($ 3) [(+4), (*10), (\x -> 2^x)]
[7,30,8]
Kann mir jemand erklären, was hier los ist?