此代码有什么问题?
rectangle :: Int -> Int -> String
rectangle i j
| i < 0 || j < 0 = ""
| otherwise = concatenate(i) ++ "n/" ++ (rectangle i) j-1
concatenate :: Int -> String
concatenate i
| i <= 0 = ""
| otherwise = "*" ++ concatenate(i-1)
这是我得到的错误
ERROR line 3 - Instance of Num [Char] required for definition of rectangle
这应该是它所做的(例如)
Main> putStr (rectangle 3 4)
****
****
****
答案 0 :(得分:3)
您迟早会习惯Haskell语法。这是
(rectangle i) j-1
被解析为
(rectangle i j) - 1
但你真的想要:
rectangle i (j-1)
答案 1 :(得分:2)
此代码应该有效。
rectangle :: Int -> Int -> String
rectangle i j
| i < 0 || j < 0 = ""
| otherwise = concatenate i ++ "\n" ++ rectangle i (j-1)
concatenate :: Int -> String
concatenate i
| i <= 0 = ""
| otherwise = "*" ++ concatenate (i-1)
很少有评论:
concatenate(i) ==> concatenate i
-- You don't need to put parameters in brackets in haskell.
(rectangle i) j-1 ==> rectangle i (j-1)
-- See Ingo's explanation
"n/" ==> "\n"
-- That should be obvious