问题描述
我有一个函数,它返回从输入列表中找到的半个回文列表。如果我在一行上使用 if 语句但我想使用守卫,它会起作用。守卫给我一个解析错误。我阅读了很多给出这种错误的案例,但我没有弄清楚我的案例。代码如下:
palindromeHalfs :: [String] -> [String]
palindromeHalfs xs = map firstHalf (filter palindrome xs)
where
firstHalf :: String -> String
firstHalf ys | (length ys) `rem` 2 == 0 = take ((div (length ys 2)) ys
| otherwise = take ((div (length ys 2) + 1) ys
palindrome :: String -> Bool
palindrome str | str == reverse str = True
| otherwise = False
和错误:
palindromeHalfs.hs:6:20: error: parse error on input `otherwise'
|
6 | | otherwise = take ((div (length ys 2) + 1) ys
| ^^^^^^^^^
如果我替换,该功能有效
firstHalf ys | (length ys) `rem` 2 == 0 = take ((div (length ys 2)) ys
| otherwise = take ((div (length ys 2) + 1) ys
与
firstHalf ys = if (length (ys !! 0)) `rem` 2 == 0 then take ((div (length (ys !! 0)) 2)) ys
else take ((div (length (ys !! 0)) 2) + 1) ys
在我的代码中,if 语句是一行,它不适合这里。如果有人能告诉我哪个是首选,如果或警卫,我将不胜感激。当然,为什么我的守卫不工作。
解决方法
括号不平衡
take ((div (length ys 2)) ys
至于样式,在可以使用任何一个的情况下,守卫比 if/else 更受欢迎。还要注意 even :: Integral a => a -> Bool
是一个存在的函数;你不必用 rem
发明它。