我一直在浏览以前问过的问题,但找不到解决我问题的答案,尽管我认为至少有一个会解决。我只是想在函数内部的字符串之间添加换行符。每当我在字符串中添加“ \ 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 " |"))
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出
"+---+---+---+---+\n| | | | |"
Run Code Online (Sandbox Code Playgroud)
我更喜欢
"+---+---+---+---+"
"| | …Run Code Online (Sandbox Code Playgroud) 我正在学习Haskell,而且我必须打印一个Snakes and Ladders游戏.刚开始,我正在尝试打印电路板,这就是我所做的.
import Data.List
aRow :: Int -> String
aRow n = "+" ++ take (4*n) (intercalate "" (repeat "---+")) ++ "\n|" ++ take (4*n) (intercalate "" (repeat " |")) ++ "\n"
board :: Int -> Int -> IO()
board 1 y = putStrLn (aRow y)
Run Code Online (Sandbox Code Playgroud)
我想要另一个董事会实例,它需要参数x和y
board x y = putStrLn (aRow y)
board (x-1) y
Run Code Online (Sandbox Code Playgroud)
我知道我不能只调用这样的多个语句,但任何人都可以提供一些有关我如何能够同意这一点的见解吗?我想用参数'y'调用aRow并执行'x'次.
谢谢.
另外:当我拨打电话板1时,我将其作为输出:电路板1 5
+ --- + --- + --- + --- + --- +
| | | | | |