isPalindrome :: Eq a => [a] -> Bool
isPalindrome [] = True
isPalindrome [a] = True
isPalindrome (x:xs) = (x == last xs) && (isPalindrome init xs)
Run Code Online (Sandbox Code Playgroud)
回报我
Couldn't match expected type `[a0]' with actual type `[a1] -> [a1]'
In the first argument of `isPalindrome', namely `init'
In the second argument of `(&&)', namely `(isPalindrome init xs)'
In the expression: (x == last xs) && (isPalindrome init xs)
Run Code Online (Sandbox Code Playgroud)
我不明白为什么Haskell认为isPalindrome的参数是init而它是init xs
在Haskell中,隐形函数应用程序操作符关联到左侧,因此isPalindrome init xs被解释为(isPalindrome init) xs.它是这样做的,因为它允许我们使用currying来处理具有多个参数的函数.
要init xs作为一个参数传递,您只需使用括号:
isPalindrome (init xs)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |