2013-12-08 67 views
0

这段代码有什么问题?Haskell数量问题实例

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

@Ingo对不起,刚刚发现并修复它,但出现同样的错误 – user2964960

+0

没有第二个参数为矩形,你​​正在做'字符串++(智力 - > String)' – afsantos

+0

“但是如果我把它放在提示符上,它会在范围'n'中表示不是。”说'让因子n ='等等。 GHCi提示符的行为就像是在一个非常大的'do'语句中(没有'do')。 – 2013-12-08 11:57:47

回答

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 
+0

@Ingo @zudov非常感谢你,只是一个随机问题,我有以下问题,但不知道该怎么做。你有什么想法吗?谢谢!定义一个函数标志模式,该函数标志模式采用大于或等于5的正的Int值,并返回一个字符串,该字符串可以显示为以下维度n的“标志”模式,例如: 主> putStr(flagpattern 7) ******* ** ** * * * * * * * * * * * ** ** ******* – user2964960

3

你会习惯Haskell语法迟早的事。在这里,这

(rectangle i) j-1 

被解析为

(rectangle i j) - 1 

但你真的想:

rectangle i (j-1) 
+0

谢谢!,得从某处开始 – user2964960