user3889372
user3889372

Reputation: 33

How to print list of tuples like table in Haskell

i have a list of tuples like:

[("3",69.46),("4",38.32),("5",111.67),("9",97.13)]

and i want to print this list of tuple like :

3  69.46
4  38.32
5  111.67
9  97.13

What is the best way to implement this? (The length of list is dynamic) Thanks

Upvotes: 0

Views: 818

Answers (1)

Sibi
Sibi

Reputation: 48746

One way would be like this:

printList xs = mapM_ (\(a,b) -> putStr a >> putStr ("  " ++ show b) >> putStrLn "") xs

Or in a more readable way:

printList xs = mapM_ (\(a,b) -> do
                          putStr a
                          putStr ("  " ++ show b)
                          putStrLn "") xs

Or as @icktoofay points out you can use a single putStrLn:

printList xs  = mapM_ (\(a,b) -> putStrLn $ a ++ " " ++ show b) xs

In 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  

Upvotes: 3

Related Questions