Haskell writes '\n' instead of a newline

早过忘川 提交于 2020-01-04 02:03:28

问题


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"

回答1:


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.



来源:https://stackoverflow.com/questions/33155110/haskell-writes-n-instead-of-a-newline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!