Haskell type error on compilation

China☆狼群 提交于 2019-12-24 19:40:38

问题


I'm not sure why the following code is causing the following error.

Code:

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 ]

Compilation Error:

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

回答1:


The code you gave has a few errors.

  1. As @FramkSchmitt mentioned there is a parameter xs missing.
  2. you try to map elem x over a list - which would need a list of lists to be correct.

here is what I would guess what you intended.

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

which can be written a bit more concise (I believe tools like hlint would even suggest this simplification).

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


来源:https://stackoverflow.com/questions/46886961/haskell-type-error-on-compilation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!