我有两个函数都返回一个列表:
enter earth = head $ do
lst <- earth
return $ map (\x -> if (x == 0) then 2 else x) lst
helper earth = head $ do
lst <- tail (earth)
return lst
see :: [Int] -> [Int] -> [Int]
see enter helper = return $ map (\ x y -> if (x == 2) && (y == 0) then 2 else y) enter helper
最佳答案
zipWith
是您正在寻找的功能。 map
一次只能在一个列表上工作,所以你使用它会导致类型错误。如果你只是替换 map
与 zipWith
在最后一行,它应该工作。此外,您应该删除 return
.在列表的上下文中,return
仅将某些内容放入单例列表中:return x = [x]
.请注意 return
是一个函数,与 return
没有真正的关系。在受 C 语言影响的语言中。我还建议不要使用 do
此时列表的符号。
此外,最后一个函数没有使用其他两个函数,即使它使用了这些名称。我不确定这是否是你想要的。相当于see
的定义您在哪里使用了名称 x
和 y
而不是 enter
和 helper
.
这是 zipWith
的行为示例:
λ> zipWith (*) [2, 3, 4] [10, 100, 1000]
[20,300,4000]
https://stackoverflow.com/questions/26704540/