在编译哈斯克尔类型的错误(Haskell type error on compilation)

2019-10-30 04:01发布

我不知道为什么下面的代码引起以下错误。

码:

type Symbol = Char

symbols :: [Symbol]
symbols = ['a'..'f']

type Code = [Symbol]

members :: Code -> Bool
members xs = and [ b | x <- xs, b <- map (elem x) symbols ]

编译错误:

Couldn't match type ‘Char’ with ‘t0 Symbol’
     Expected type: [t0 Symbol]
       Actual type: [Symbol]
   • In the second argument of ‘map’, namely ‘symbols’
     In the expression: map (elem x) symbols
     In a stmt of a list comprehension: b <- map (elem x) symbols

Answer 1:

你给的代码有一些错误。

  1. 由于@FramkSchmitt提到有一个参数xs失踪。
  2. 您尝试映射elem x在一个列表-这将需要一个列表的列表是正确的。

这里是我猜你的原意。

members :: Code -> Bool
members xs = and [ x `elem` symbols  | x <- xs ]

它可以更简洁一点书面(我相信像hlint工具甚至建议这种简化)。

members' :: Code -> Bool
members' = all (`elem` symbols)


文章来源: Haskell type error on compilation