【发布时间】:2017-10-24 00:57:11
【问题描述】:
推理Haskell type error on compilation 中给出的错误我发现 GHCi 的一个奇怪行为
test.hs
members :: String -> Bool;
members str = and [ b | x <- str, b <- map (elem x) "abcde" ]
产生以下错误信息
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"
而
GHCi
Prelude> members :: String -> Bool; members xs = and [ b | x <- xs, b <- map (elem x) "abcde"]
产生
<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"
更新
我也不明白,为什么 GHC 省略了 Foldable t0 类约束 - 我
本来期望错误消息类似于:
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"
【问题讨论】:
标签: haskell compiler-errors ghc ghci