Singularity
Singularity

Reputation: 35

How to print elements of A list of Ints in one line in Haskell

I need to print a list of integers in Haskell in one line only separated by space... Like , I would want to print

[6,5,4,7,3,9]

in this manner

6 5 4 7 3 9

I used the Map_ function but that prints all the elements in different lines.

Upvotes: 0

Views: 886

Answers (2)

Z-Y.L
Z-Y.L

Reputation: 1779

You can define a customized print function, which accepts a parameter of separator:

type Separator = String
cPrint :: Show a => Separator -> a -> IO ()
cPrint sep v = putStr $ show v ++ sep

ghci> mapM_ (cPrint " ") [6,5,4,7,3,9]
6 5 4 7 3 9 ghci>

Upvotes: 0

ziggystar
ziggystar

Reputation: 28680

You can try this

main = putStrLn . unwords . map show $ [1,2,3,4]

Instead of unwords you could also use intercalate " " for a more generic function.

Upvotes: 5

Related Questions