我在寫一個名爲printField的函數。該函數以int
和string
作爲參數,然後使用以下內容打印出如下這樣的字段:printField 7 "Derp"
。當該字段由數字組成時,輸出應該是「... 3456」。在haskell中使用額外的點打印字段
我寫的函數如下所示:
printField :: Int -> String -> String
printField x y = if isDigit y
then concat(replicate n ".") ++ y
else y ++ concat(replicate n ".")
where n = x - length y
這顯然是行不通的。我從GHC得到的錯誤是:?
Couldn't match type `[Char]' with `Char'
Expected type: Char
Actual type: String
In the first argument of `isDigit', namely `y'
In the expression: isDigit y
In the expression:
if isDigit y then
concat (replicate n ".") ++ y
else
y ++ concat (replicate n ".")
我不能讓它的工作:(誰能幫助我,請記住,我是新來的Haskell和一般功能的編程
'isDigit'的類型爲'Char - > Bool',但'y'的類型爲'String',它是'[Char]'的別名,所以'isDigit y'不會檢查類型。 – Lee