如何解决输入“否则”的 Haskell 解析错误

Jon*_*nne 0 haskell parse-error guard-clause

我有一个函数,它返回从输入列表中找到的半个回文列表。如果我在一行上使用 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 

Run Code Online (Sandbox Code Playgroud)

和错误:

palindromeHalfs.hs:6:20: error: parse error on input `otherwise'
  |
6 |                  | otherwise                = take ((div (length ys 2) + 1) ys
  |                    ^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

如果我更换,该功能有效

firstHalf ys | (length ys) `rem` 2 == 0 = take ((div (length ys 2)) ys
             | otherwise                = take ((div (length ys 2) + 1) ys
Run Code Online (Sandbox Code Playgroud)

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

Run Code Online (Sandbox Code Playgroud)

在我的代码中,if 语句是一行,它不适合这里。如果有人能告诉我哪个是首选,如果或警卫,我将不胜感激。当然,为什么我的守卫不起作用。

ama*_*loy 7

括号不平衡

take ((div (length ys 2)) ys
Run Code Online (Sandbox Code Playgroud)

至于样式,在可以使用任何一个的情况下,守卫比 if/else 更受欢迎。还要注意,这even :: Integral a => a -> Bool是一个存在的函数;你不必用rem.