I have this code and instead of it printing out "\n", I want it to put the next string on a new line, but cannot seem to figure it out. Any pointers?
onSeparateLines :: [String] -> String
onSeparateLines [] = ""
onSeparateLines ( x:[] ) = x
onSeparateLines ( x:xs ) = x ++ "\n" ++ onSeparateLines xs
what I get is
"AAAA\nAAAA"
which should be:
"AAAA"
"AAAA"
The given function and your use of "\n" are correct, so the error must be elsewhere. Without knowing the details, I suspect that you are using (the equivalent of) print rather than putStr to print your string. Make sure that your string is not being shown before it is printed.
If this is in GHCi, be aware that values are printed using print, so
> onSeparateLines ["foo", "bar"]
will print the string and show escaped characters. You want
> putStrLn (onSeparateLines ["foo", "bar"])
instead.
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