Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 新行缩进Haskell_String_Haskell_Indentation - Fatal编程技术网

String 新行缩进Haskell

String 新行缩进Haskell,string,haskell,indentation,String,Haskell,Indentation,我是Haskell的新手,有以下问题: 我打算创建一个函数,它将三个字符串放在不同的行上。代码如下: onThreeLines :: String -> String -> String -> String onThreeLines a b c = a++"\n"++b++"\n"++c 以下是我所运行的: onThreeLines "Life" "is" "wonderful" 我得到的是: "Life\nis\nwonderful" 我也尝试过以下角色,但效果不

我是Haskell的新手,有以下问题: 我打算创建一个函数,它将三个字符串放在不同的行上。代码如下:

onThreeLines :: String -> String -> String -> String
onThreeLines a b c = a++"\n"++b++"\n"++c
以下是我所运行的:

  onThreeLines "Life" "is" "wonderful"
我得到的是:

 "Life\nis\nwonderful"
我也尝试过以下角色,但效果不太好

"'\n'"

你的功能正常。如果您在GHCi中运行此操作或使用
print
,您可能会被以下事实所迷惑:它对计算结果调用
show
,将值格式化为Haskell术语以进行调试。对于字符串,这意味着包含引号和转义符


putStrLn(在录影带上,“生活”是“美妙的”)
应该完全按照你的期望去做。

这样执行应该可以让它工作:

main :: IO ()
main = putStrLn $ onThreeLines "hello" "world" "test"
执行程序时,我得到:

$ ./test.hs
hello
world
test
您获得
“Life\nis\nOnDerful”
的原因是
Show
实例用于显示将转义换行符的内容

λ> putStrLn "hello\nworld"
hello
world
λ> print "hello\nworld"
"hello\nworld"

请注意,
print
使用
Show
实例进行显示。

您的函数没有问题。“Life\nis\nOnDerful”是所需的结果字符串。请记住,如果希望正确呈现换行符,请将其传递给函数,如
putStrLn

putStrLn (onThreeLines "Life" "is" "wonderful")
另外,一定要检查
unlines
函数,该函数连接字符串列表,用换行符分隔每个元素