How do I print a list to stdout in Haskell?
Let's say I have a list [1,2,3]
and I want to convert that list into a string and print it out. I guess I could build my own function, but surely Haskell has a function built in to do that.
Example #1print("Demo to show list in Haskell !!") let list1 = [100, 50, 235, 167, 345, 909, 675, 20] let list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9] let list3 = [1.1, 2.2, 3.3, 4.4, 5.5, 6.6] let list4 = [5, 10, 15, 20, 15, 30, 35, 40] let list5 = [123.4, 567.9, 56.0, 3.9, 76.9] print("printing list element !!")
If you have a String that you want to print to the screen, you should use putStrLn . If you have something other than a String that you want to print, you should use print . Look at the types! putStrLn :: String -> IO () and print :: Show a => a -> IO () .
Indeed there is a built in function, aptly named print
.
> print [1,2,3] [1,2,3]
This is equivalent to putStrLn $ show [1,2,3]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With