【发布时间】:2016-05-01 06:43:41
【问题描述】:
我正在尝试找出一种方法,我可以使用如图所示的 haskell 创建一个带有孔的矩形
Main > putStr (hollowedrectangle 4 5)
*****
* *
* *
*****
但是,我知道下面的代码首先实现了 topnbot 行,因此它当前在中间返回一个完整的星号行(topnbot)。我试图弄清楚我需要在我的代码中更改什么,以便它给出上面的结果,而不是下面的结果。
topnbot :: Int->String
topnbot x
| x <= 0 = ""
| x == 1 = "*"++topnbot(x-1)
| x > 0 = "*"++topnbot(x-1)
leftnright :: Int->Int->String
leftnright x y
| y==1 = "*"
| x < 0 = ""
| x >= 0 = "*"++mid (y-1)
where
mid :: Int->String
mid z
| z <= 0 = ""
| z == 1 = leftnright x z
| z > 0 = " "++mid (z-1)
hollowedrectangle :: Int->Int->String
hollowedrectangle a m
| m == 0 = topnbot m
| a == 0 = ""
| a < 3 = topnbot m
| a >= 3 && m >= 3 = topnbot m ++"\n"++leftnright a m ++ "\n" ++hollowedrectangle (a-1) m
这会导致
Main > putStr (hollowedrectangle 4 5)
*****
* *
*****
* *
*****
谢谢!
【问题讨论】: