1
Wenn SieHaskell Funktion Typ Variablen
foo :: a -> a -> Bool
Enthält diese beide Arten erzwingen „a“ sind die gleichen?
Wenn SieHaskell Funktion Typ Variablen
foo :: a -> a -> Bool
Enthält diese beide Arten erzwingen „a“ sind die gleichen?
Ja. Sie können dies mit einer Funktion beobachten, die ansonsten ihre Argumente ignoriert.
foo :: a -> a -> Bool
foo _ _ = True
Aufruf mit zwei Argumenten des gleichen Typs funktioniert.
Das Aufrufen von zwei Argumenten unterschiedlicher Typen führt zu einem Typfehler, der genaue Fehler hängt davon ab, welche Typen Sie auswählen.
Prelude> foo 1 'x'
<interactive>:5:5:
No instance for (Num Char) arising from the literal ‘1’
In the first argument of ‘foo’, namely ‘1’
In the expression: foo 1 'x'
In an equation for ‘it’: it = foo 1 'x'
Prelude> foo 'x' (1::Int)
<interactive>:8:10:
Couldn't match expected type ‘Char’ with actual type ‘Int’
In the second argument of ‘foo’, namely ‘(1 :: Int)’
In the expression: foo 'x' (1 :: Int)
In an equation for ‘it’: it = foo 'x' (1 :: Int)
Prelude> foo (1::Int) 'x'
<interactive>:9:14:
Couldn't match expected type ‘Int’ with actual type ‘Char’
In the second argument of ‘foo’, namely ‘'x'’
In the expression: foo (1 :: Int) 'x'
In an equation for ‘it’: it = foo (1 :: Int) 'x'
Ja. –
Alec