爲什麼這個代碼不工作:檢查字符串是否持有整數
import IO
import Char
isInteger "" = False
isInteger (a:b) =
if length b == 0 && isDigit(a) == True then True
else if isDigit(a) == True then isInteger(b)
else False
main = do
q <- getLine
let x = read q
if isInteger x == False then putStrLn "not integer"
else putStrLn "integer"
'isInteger s = not(null s)&& all isDigit s' :-) – luqui 2011-04-17 08:45:30
另請注意,'x == True'和'x'是一樣的。 '(== True)'是身份函數。所以例如。第一行可以是'如果長度b == 0 && isDigit a then True'。但是......你仍然在努力工作:-) – luqui 2011-04-17 08:47:14
哇 - 非常有趣的解決方案,非常感謝你:) – mrquestion 2011-04-17 09:13:45