我有一个函数,它返回从输入列表中找到的一半回文列表。如果我在一行上使用 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 语句是一行,它不适合这里。如果有人能告诉我哪个是首选,如果或守卫,我将不胜感激。当然,为什么我的警卫不起作用。