如何在Haskell中打印像表这样的元组列表

时间:2014-08-24 02:32:57

标签: 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

实现此目的的最佳方法是什么? (列表的长度是动态的) 感谢

1 个答案:

答案 0 :(得分:3)

一种方式是这样的:

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