Reputation: 35
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
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
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