haskell - Haskell 中的新行

标签 haskell functional-programming newline

我一直在浏览以前提出的问题,但找不到解决我的问题的答案,尽管我认为至少其中一个可以解决。我只是想在函数内部的字符串之间添加换行符。每当我向字符串添加“\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 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.

长话短说,您可能需要使用 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/

相关文章:

c++ - 如何在用户输入 ^X 之前读取输入

c - 如果从文件输入中获取,为什么不\n 生成一个新行字符?

haskell - 如何在 Haskell 中向 Functor 实例声明添加类约束?

list - Haskell类型的数学问题

functional-programming - 什么是指称语义?

c - 为什么我的 '+' 运算符被视为换行符?

haskell - 超时时如何重试阻塞IO Action?

haskell - 检查列表是否为回文的数学函数

javascript - angular.identity() 有什么用例的好例子吗?

javascript - Map 或 flatmap 的工作方式类似于 Javascript 中的 Array.prototype.join