我有一个包含3个字符串的列表列表,如 tst3 ,我需要解析所有列表并使用 function 中的第一个和第二个参数(字符串) 。但地图不适合它
function a b = do {putStrLn (a ++ "stuff"); putStrLn b;} tst3 = [["aaa","aaaaaaaa","112121"],["asda","a22","aaax"]] fx2 s = map fx3 (tst3) where fx3 s = function (s!!0)(s!!1) Couldn't match expected type `[a]' against inferred type `Char' Expected type: [[[a]]] Inferred type: [[Char]] In the second argument of `map', namely `(tst3)' In the expression: map fx3 (tst3)
有更好的方法吗? 如果变得容易,我可以使用[(“aa”,“bb”),(“ww”,“cc”),(“jj”,“oooo”)]
由于
答案 0 :(得分:3)
你所写的内容没有错误,但实际上并没有什么意义。您正在将某种数据列表转换为IO操作列表,但仅此一项不会执行任何操作:您可能需要的是对列表的每个元素执行这些操作。通常使用mapM_
:
function :: String -> String -> IO()
function a b = do putStrLn (a ++ "stuff") -- there's no reason to use curly
putStrLn b -- brackets here
tst::[(String,String,String)]
tst3 = [("aaa","aaaaaaaa","112121"),("asda","a22","aaax")]
fx2 :: a -> IO()
fx2 s = mapM_ fx3 (tst3) -- note that this s parameter is not used at all
where fx3 (s,s',_) = function s s'