Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newline in Haskell String?

How can I create a newline inside a String? Is it possible without using IO ()?

formatRow :: Car -> String formatRow (a, d:ds, c, x:xs) = a ++ " | " ++ x ++ concat xs ++ " | " ++ show c ++ " | " ++ d ++ concat ds ++ (show '\n') 
like image 872
Ash Avatar asked May 10 '11 00:05

Ash


People also ask

What does lines do in Haskell?

lines breaks a ByteString up into a list of ByteStrings at newline Chars ('\n'). The resulting strings do not contain newlines.

What does putStr do Haskell?

putStr is much like putStrLn in that it takes a string as a parameter and returns an I/O action that will print that string to the terminal, only putStr doesn't jump into a new line after printing out the string while putStrLn does. putStrLn "Andy!"


1 Answers

To create a string containing a newline, just write "\n".

If you run your program on Windows, it will automatically be converted to "\r\n".

Note that calling show on it will escape the newline (or any other meta-characters), so don't do foo ++ (show "\n") or foo ++ (show '\n') - just use foo ++ "\n".

Also note that if you just evaluate a string expression in GHCi without using putStr or putStrLn, it will just call show on it, so for example the string "foo\n" will display as "foo\n" in GHCi, but that does not change the fact that it's a string containing a newline and it will print that way, once you output it using putStr.

like image 159
sepp2k Avatar answered Oct 14 '22 21:10

sepp2k