我知道我能做到这一点......
isZero :: Int -> Bool
isZero x
| x == 0 = True
| otherwise = False
Run Code Online (Sandbox Code Playgroud)
但我可以这样做吗?
isPalindrome :: Int -> Bool
isPalindrome x
let digitList = intToDigits x -- Decomposes the integer into
-- digits, i.e. 37 -> [3, 7]
| digitList == reverse digitList = True
| otherwise = False
Run Code Online (Sandbox Code Playgroud)
这将导致编译错误,但我确信你知道我正在尝试做什么.
ham*_*mar 14
请改用where子句
isPalindrome :: Int -> Bool
isPalindrome x
| digitList == reverse digitList = True
| otherwise = False
where digitList = intToDigits x
Run Code Online (Sandbox Code Playgroud)
当然,对于这个例子,我们可以跳过警卫并写下来
isPalindrome x = digitList == reverse digitList
where digitList = intToDigits x
Run Code Online (Sandbox Code Playgroud)