如何在Haskell中打印类似表的元组列表
我有一个元组列表,如:如何在Haskell中打印类似表的元组列表,haskell,Haskell,我有一个元组列表,如: [("3",69.46),("4",38.32),("5",111.67),("9",97.13)] 我想打印这个元组列表,如下所示: 3 69.46 4 38.32 5 111.67 9 97.13 实现这一点的最佳方式是什么? (列表的长度是动态的) 谢谢一种方法是: printList xs = mapM_ (\(a,b) -> putStr a >> putStr (" " ++ show b) >> putStrLn
[("3",69.46),("4",38.32),("5",111.67),("9",97.13)]
我想打印这个元组列表,如下所示:
3 69.46
4 38.32
5 111.67
9 97.13
实现这一点的最佳方式是什么?
(列表的长度是动态的)
谢谢一种方法是:
printList xs = mapM_ (\(a,b) -> putStr a >> putStr (" " ++ show b) >> putStrLn "") xs
或者以更可读的方式:
printList xs = mapM_ (\(a,b) -> do
putStr a
putStr (" " ++ show b)
putStrLn "") xs
或者正如@icktoofay指出的,您可以使用一个putStrLn
:
printList xs = mapM_ (\(a,b) -> putStrLn $ a ++ " " ++ show b) xs
在ghci中:
λ> printList [("3",69.46),("4",38.32),("5",111.67),("9",97.13)]
3 69.46
4 38.32
5 111.67
9 97.13
一种方法是这样的:
printList xs = mapM_ (\(a,b) -> putStr a >> putStr (" " ++ show b) >> putStrLn "") xs
或者以更可读的方式:
printList xs = mapM_ (\(a,b) -> do
putStr a
putStr (" " ++ show b)
putStrLn "") xs
或者正如@icktoofay指出的,您可以使用一个putStrLn
:
printList xs = mapM_ (\(a,b) -> putStrLn $ a ++ " " ++ show b) xs
在ghci中:
λ> printList [("3",69.46),("4",38.32),("5",111.67),("9",97.13)]
3 69.46
4 38.32
5 111.67
9 97.13
一种方法是这样的:
printList xs = mapM_ (\(a,b) -> putStr a >> putStr (" " ++ show b) >> putStrLn "") xs
或者以更可读的方式:
printList xs = mapM_ (\(a,b) -> do
putStr a
putStr (" " ++ show b)
putStrLn "") xs
或者正如@icktoofay指出的,您可以使用一个putStrLn
:
printList xs = mapM_ (\(a,b) -> putStrLn $ a ++ " " ++ show b) xs
在ghci中:
λ> printList [("3",69.46),("4",38.32),("5",111.67),("9",97.13)]
3 69.46
4 38.32
5 111.67
9 97.13
一种方法是这样的:
printList xs = mapM_ (\(a,b) -> putStr a >> putStr (" " ++ show b) >> putStrLn "") xs
或者以更可读的方式:
printList xs = mapM_ (\(a,b) -> do
putStr a
putStr (" " ++ show b)
putStrLn "") xs
或者正如@icktoofay指出的,您可以使用一个putStrLn
:
printList xs = mapM_ (\(a,b) -> putStrLn $ a ++ " " ++ show b) xs
在ghci中:
λ> printList [("3",69.46),("4",38.32),("5",111.67),("9",97.13)]
3 69.46
4 38.32
5 111.67
9 97.13
+1,但我会让它自由点并使用do表示法:
mapM\u$\(a,b)->do…
@nomen包含了一个基于do表示法的代码。无点工具给了我这个:mapM(uncurry(flip-flip-putStrLn.(>>)。((putStr.flip(++).show))。(>>).putStr))xs
我发现这很难阅读。是的,这太多了。我只是想去掉printList
的参数。为什么不只使用putStr
一次,例如printList=mapM\u$\(a,b)->putStrLn$a+++++++++show b
?@icktoofay是的,这要简单得多。更新了它。+1,但我会让它自由点并使用do表示法:mapM\u$\(a,b)->do…
@nomen包含了一个基于do表示法的代码。无点工具给了我这个:mapM(uncurry(flip-flip-putStrLn.(>>)。((putStr.flip(++).show))。(>>).putStr))xs
我发现这很难阅读。是的,这太多了。我只是想去掉printList
的参数。为什么不只使用putStr
一次,例如printList=mapM\u$\(a,b)->putStrLn$a+++++++++show b
?@icktoofay是的,这要简单得多。更新了它。+1,但我会让它自由点并使用do表示法:mapM\u$\(a,b)->do…
@nomen包含了一个基于do表示法的代码。无点工具给了我这个:mapM(uncurry(flip-flip-putStrLn.(>>)。((putStr.flip(++).show))。(>>).putStr))xs
我发现这很难阅读。是的,这太多了。我只是想去掉printList
的参数。为什么不只使用putStr
一次,例如printList=mapM\u$\(a,b)->putStrLn$a+++++++++show b
?@icktoofay是的,这要简单得多。更新了它。+1,但我会让它自由点并使用do表示法:mapM\u$\(a,b)->do…
@nomen包含了一个基于do表示法的代码。无点工具给了我这个:mapM(uncurry(flip-flip-putStrLn.(>>)。((putStr.flip(++).show))。(>>).putStr))xs
我发现这很难阅读。是的,这太多了。我只是想去掉printList
的参数。为什么不只使用putStr
一次,例如printList=mapM\u$\(a,b)->putStrLn$a+++++++++show b
?@icktoofay是的,这要简单得多。更新了它。