Reasoning about an error given in Haskell type error on compilation I found a strange behaviour of GHCi
test.hs
members :: String -> Bool;
members str = and [ b | x <- str, b <- map (elem x) "abcde" ]
produces the following error message
Prelude> :l test.hs
[1 of 1] Compiling Main ( test.hs, interpreted )
test.hs:2:53: error: • Couldn't match type ‘Char’ with ‘t0 Char’ Expected type: [t0 Char] Actual type: [Char] • In the second argument of ‘map’, namely ‘"abcde"’ In the expression: map (elem x) "abcde" In a stmt of a list comprehension: b <- map (elem x) "abcde"
whereas
GHCi
Prelude> members :: String -> Bool; members xs = and [ b | x <- xs, b <- map (elem x) "abcde"]
produces
<interactive>:18:78: error: • Couldn't match type ‘Char’ with ‘[Char]’ Expected type: [[Char]] Actual type: [Char] • In the second argument of ‘map’, namely ‘"abcde"’ In the expression: map (elem x) "abcde" In a stmt of a list comprehension: b <- map (elem x) "abcde"
Update
What I also do not understand, is why GHC omits the Foldable t0 class constraint - I
would have expected the error message be something like:
test.hs:2:53: error: • Couldn't match type ‘Char’ with ‘t0 Char’ Expected type: Foldable t0 => [t0 Char] Actual type: [Char] • In the second argument of ‘map’, namely ‘"abcde"’ In the expression: map (elem x) "abcde" In a stmt of a list comprehension: b <- map (elem x) "abcde"