问题
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.
- As @FramkSchmitt mentioned there is a parameter
xs
missing. - 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