Paul Reiners
Paul Reiners

Reputation: 7894

Haskell - Printing elements of a tuple

I know that I can print line-by-line the tuples in a list of tuples like this:

Prelude> mapM_ print [(1, 1), (2, 4), (3, 9)]
(1,1)
(2,4)
(3,9)

But suppose that I want to output this to a CSV file and I want to output this

Prelude> ??? [(1, 1), (2, 4), (3, 9)]
1,1
2,4
3,9

How can I do that?

Upvotes: 1

Views: 1755

Answers (1)

ThreeFx
ThreeFx

Reputation: 7360

Try this:

showTup :: (Show a, Show b) => (a,b) -> String
showTup (a,b) = (show a) ++ "," ++ (show b)

λ> mapM_ (putStrLn . showTup) [(1,1), (2,4), (3,9)]
1,1
2,4
3,9

Since Haskell is so awesome, you can just write a function that converts a tuple to a string, and since print is just (putStrLn . show) you can substitute show by your own function.

Upvotes: 7

Related Questions