gpt4 book ai didi

haskell - Init 被错误地当作参数

转载 作者:行者123 更新时间:2023-12-02 14:26:04 25 4
gpt4 key购买 nike

isPalindrome :: Eq a => [a] -> Bool
isPalindrome [] = True
isPalindrome [a] = True
isPalindrome (x:xs) = (x == last xs) && (isPalindrome init xs)

返回我

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)

我不明白为什么Haskell认为isPalindrome的参数是init,而它是init xs

最佳答案

在 Haskell 中,不可见函数应用运算符关联到左侧,因此 isPalindrome init xs 被解释为 (isPalindrome init) xs。这样做是因为它允许我们使用 currying处理具有多个参数的函数。

要将 init xs 作为一个参数传递,您只需使用括号即可:

isPalindrome (init xs) 

关于haskell - Init 被错误地当作参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21774147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com