import Data.Foldable (for_)
import Data.Map (Map,toList)
m :: Map String String
m = [("a","1"),("b","2")]
main =
for_ (toList m) $ \(q,a) ->
do putStrLn q
x <- getLine
putStrLn (if x == a
then "Yes"
else "No: " ++ a)
我收到错误: -
foo.hs:5:5: error:
* Couldn't match expected type `Map String String'
with actual type `[([Char], [Char])]'
* In the expression: [("a", "1"), ("b", "2")]
In an equation for `m': m = [("a", "1"), ("b", "2")]
Failed, modules loaded: none.
感谢您的帮助。我对for_i解决方案感兴趣,但我无法加载Control.Lens
答案 0 :(得分:4)
答案 1 :(得分:2)
Map String String
不仅仅是元组列表的别名;它是一种独特的数据类型,因此您的分配不会进行类型检查。您需要使用函数Data.Map.fromList
将列表转换为正确的Map k v
值。
m = Data.Map.fromList [("a","1"),("b","2")]