Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Haskell中打印类似表的元组列表_Haskell - Fatal编程技术网

如何在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是的,这要简单得多。更新了它。