我一直在浏览以前提出的问题,但找不到解决我的问题的答案,尽管我认为至少其中一个可以解决。我只是想在函数内部的字符串之间添加换行符。每当我向字符串添加“\n”时,它只会打印“\n”
import Data.List
-- aRow takes number of columns as argument
-- The idea is to use this function with the number of columns as argument.
-- Example, if we want 3 columns, we'd say aRow 3, and get "+---+---+---+"
aRow :: Int -> String
aRow n = "+" ++ take (4*n) (intercalate "" (repeat "---+")) ++ "\n|" ++ take (4*n) (intercalate "" (repeat " |"))
这是我得到的输出
"+---+---+---+---+\n| | | | |"
我更喜欢
"+---+---+---+---+"
"| | | | |"
各行位于不同的行上(竖线之间应该有 3 个空格,请忽略我的格式。我主要是想让换行符正常工作)。谢谢。
最佳答案
https://stackoverflow.com/a/5944062/1848654 :
If you just evaluate a string expression in ghci without using
putStr
orputStrLn
, 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 usingputStr
.
长话短说,您可能需要使用 putStr
,因为 Haskell 默认在该字符串上使用 show
并显示 \n
> 很明显,就像它在这里为您所做的那样。
示例:
import Data.List
main = putStrLn(aRow 4)
aRow :: Int -> String
aRow n = "+" ++ take (4*n) (intercalate "" (repeat "---+")) ++ "\n|" ++ take (4*n) (intercalate "" (repeat " |"))
关于haskell - Haskell 中的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43032227/