回答
map (map f) l
其中f :: Char -> Foo
是如果你想要一个功能f
适用于像这样的列表中的每个元素的功能应用到每个Char
和l :: [[Char]]
回报l' :: [[Foo]]
:
[a, b, c, d] → [f a, f b, f c, f d]
然后map f xs
有窍门。 map
将元素上的函数转换为列表上的函数。因此,我们可以将它嵌套在列表中进行操作:如果f
将a
s转换为b
s,则map (map f)
将[[a]]
转换为[[b]]
s。
如果你不是要为列表中的每个元素(这更像是传统的迭代)执行一些IO动作,那么你很可能在寻找forM_
:
forM_ :: [a] -> (a -> IO b) -> IO()
你给它一个函数,并按顺序与列表中的每个元素进行调用。例如,forM_ xs putStrLn
是一个IO操作,它将自行打印出xs
中的每个字符串。下面是一个更复杂的使用forM_
的例子:
main = do
...
forM_ xs $ \s -> do
putStrLn "Here's a string:"
forM_ s print
putStrLn "Now it's done."
如果xs
包含["hello", "world"]
,那么这将打印出:
Here's a string:
'h'
'e'
'l'
'l'
'o'
Now it's done.
Here's a string:
'w'
'o'
'r'
'l'
'd'
Now it's done.
forM_
实际上有一个更一般的类型,但更简单我在这里展示的版本更具相关性。
迭代的“正确”方式实际上是折叠。任何你可能想要做的事都可以通过折叠来完成。让我们考虑你想做什么。你可能在想的是这样的:
for (row in xs):
for (c in row):
doSomething
的问题是,你可能利用可变变量的doSomething
。没关系,我们可以解决这个问题。所以假设你有这个。
def iter2d(xs):
outerVar = outerInit
for (row in xs):
innerVar = innerInit(row)
outerVar.adjust1(row)
for (c in row):
innerVar.adjust2(c)
outerVar.adjust3(c, innerVar)
return outerVar
让我们把它翻译成折叠。和不变性。
iter2d :: [[Char]] -> Something
iter2d xs = foldl' outerStep outerInit xs
where outerInit = ... -- same as outerInit above
outerStep acc row = fst $ foldl' innerStep innerInit' row)
where innerInit' = ((adjust1 acc row), innerInit row)
innerInit row = ... -- same as innerInit above
innerStep (outAcc, inAcc) c = (outAcc', inAcc')
where inAcc' = adjust2 inAcc c
outAcc' = adjust3 outAcc c inAcc'
与永恒的通知,我们不得不指出outAc'
取决于inAcc'
,而不是inAcc
,意,它的更新innerVar
后,“状态”。
现在你可能会说“哇哈斯克尔看起来很丑,为什么我会想要使用哈斯克尔”。是的,它看起来很丑陋,但仅仅因为我将它定制为命令式代码的直接翻译。一旦你习惯于使用折叠而不是“遍历列表”,那么你会发现折叠是一个非常强大的技术,它可以让你以比环允许更优雅的方式做很多事情。
注意:map是fold的一个特例。 –
只是:
[c | x <- xs, c <- x]
- 1. Haskell预订遍历树到列表
- 2. 如何遍历Haskell中的列表列表
- 3. 树与Haskell遍历
- 4. 遍历列表
- 5. 遍历列表
- 6. 遍历列表
- 7. 遍历列表
- 8. 遍历列表
- 9. 遍历列表的列表?
- 10. 遍历MySQL表中的列
- 11. 遍历oracle表中的列
- 12. 遍历javascript中的列表
- 13. 遍历SharePoint列表中的列表2013
- 14. Jquery遍历列表
- 15. QTreeview遍历列表
- 16. 遍历列表python
- 17. 遍历类列表
- 18. 遍历表列-jQuery
- 19. 遍历数列表
- 20. Prolog遍历列表
- 21. Python遍历列表
- 22. 遍历列表(Python)
- 23. 遍历列表C#
- 24. JQuery遍历表列
- 25. Haskell n元树遍历
- 26. Haskell - 树遍历:预订
- 27. Haskell中的场景图遍历
- 28. 的Haskell - 遍历树,并输出到列表
- 29. 遍历列表的Python
- 30. 的Python:遍历列表
我们可以给予更具体的建议,如果你还解释*你想要的东西,而“迭代”的事情。 –