list Haskell 遍历列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7564103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Haskell iterate over a list
提问by nobody
I know you suppose to think differently in Haskell, but can someone give me a quick answer on how to iterate over a list or nested list and print out a character based on the value of the list element.
我知道你想在 Haskell 中有不同的想法,但是有人可以快速回答我如何迭代列表或嵌套列表并根据列表元素的值打印出一个字符。
list1 = [[1 0 0][0 1 0][0 0 1]]
By iterate through this nested list, it should print out x for 0 and y for 1
通过迭代这个嵌套列表,它应该打印出 x 为 0 和 y 为 1
yxx
xyx
xxy
Thanks
谢谢
回答by ivanm
First of all, I think you mean:
首先,我认为你的意思是:
list1 :: [[Int]]
list1 = [[1,0,0],[0,1,0],[0,0,1]]
As for what you want:
至于你想要什么:
valueOf :: Int -> Char
valueOf 0 = 'x'
valueOf 1 = 'y'
valueOf _ = 'z'
listValues :: [[Int]] -> [String]
listValues = map (map valueOf)
printValues :: [[Int]] -> IO ()
printValues = putStrLn . unlines . listValues
And then in ghci:
然后在 ghci 中:
*Main> printValues list1
yxx
xyx
xxy
回答by franza
Try this:
尝试这个:
fun :: [[Int]] -> [String]
fun = (map . map) (\x -> if x == 0 then 'x' else 'y')
If you really need printing of result:
如果您确实需要打印结果:
printSomeFancyList :: [[Int]] -> IO ()
printSomeFancyList = putStrLn . unlines . fun
回答by Carsten
define f by something like
通过类似的东西定义 f
f x = if x == 0 then 'x' else 'y'
then
然后
map (map f) [[1,0,0],[0,1,0],[0,0,1]]
is what you want or if you want it fancier:
是你想要的,或者如果你想要更高级:
map' = map.map
map' f [[1,0,0],[0,1,0],[0,0,1]]
回答by Satvik
iterateList = foldl1 (>>).concat.intersperse [putStrLn ""].(map.map) (\c -> putStr $ if (c==0) then "X" else "Y")
回答by Zopa
The solutions using map
are the preferred Haskell style. But while you're learning, you may find explicit recursion easier to follow. Like so:
使用的解决方案map
是首选的 Haskell 风格。但是在学习的过程中,您可能会发现显式递归更容易理解。像这样:
charSub :: Int -> Char
charSub 0 = 'x'
charSub 1 = 'y'
charSub x = error "Non-binary value!"
listSub :: [Int] -> String
listSub [] = []
listSub (x:xs) = (charSub x) : (listSub xs)
nestedSub :: [[Int]] -> String
nestedSub [] = []
nestedSub (y:ys) = (listSub y) ++ "\n" ++ (nestedSub ys)
map
does pretty much the same thing--it applies a function to each element in a list. But it may be easier to see what's going on here.
map
做几乎相同的事情——它对列表中的每个元素应用一个函数。但可能更容易看到这里发生了什么。
回答by MarcoS
If you are interested in arbitrary nested lists, then you can write something like this (an arbitrary nested list is essentially a tree):
如果您对任意嵌套列表感兴趣,那么您可以编写如下内容(任意嵌套列表本质上是一棵树):
data Nested a = Leaf a | Nest [Nested a] deriving Show
traverse :: Nested Integer -> Nested Char
traverse (Leaf x) = Leaf (valueOf x)
traverse (Nest xs) = Nest (map traverse xs)
valueOf :: Integer -> Char
valueOf 0 = 'x'
valueOf 1 = 'y'
valueOf _ = 'z'
With that you can do:
有了它,你可以:
Main> let nl = Nest [Leaf 1, Leaf 0, Nest [Leaf 0, Leaf 0, Leaf 1, Nest [Leaf 1, Leaf 1, Leaf 0]], Nest [Leaf 1, Leaf 1]]
Main> traverse nl
Nest [Leaf 'y',Leaf 'x',Nest [Leaf 'x',Leaf 'x',Leaf 'y',Nest [Leaf 'y',Leaf 'y',Leaf 'x']],Nest [Leaf 'y',Leaf 'y']]
The function traverse
takes an arbitrary nested list of Integer
s and returns a corresponding nested list of Char
s according to the valueOf
rule
该函数traverse
采用Integer
s的任意嵌套列表,并Char
根据valueOf
规则返回相应的s嵌套列表
回答by jmejia
The solutions
解决方案
cambiar = putStr.unlines.(map (map f)) where f x = if x == 0 then 'x' else 'y'