我知道我可以这样做......
isZero :: Int -> Bool
isZero x
| x == 0 = True
| otherwise = False
但我可以这样做吗?
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
这会导致编译错误,但我相信你知道我在做什么。
答案 0 :(得分:14)
使用where
子句
isPalindrome :: Int -> Bool
isPalindrome x
| digitList == reverse digitList = True
| otherwise = False
where digitList = intToDigits x
当然,对于这个例子,我们可以跳过警卫并写下
isPalindrome x = digitList == reverse digitList
where digitList = intToDigits x
答案 1 :(得分:3)
为什么不
isPalindrome x = digitList == reverse digitList
where digitList = intToDigits x