是的,另一个美元符号问题。对不起……(我用了搜索功能!)
我的函数式编程课程教授告诉我们,美元符号“有点添加一个左括号,然后在末尾添加一个右括号”(这里以或多或少相同的方式非常粗略地描述)。所以
fibs = 0 : 1 : zipWith (+) fibs $ tail fibs
应该相当于
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
好吧,它不是。第二件事编译得很好,第一件事给出了一个错误:
jkjj.hs:1:8:
Couldn't match expected type `[a1] -> [a1]' with actual type `[a0]'
The first argument of ($) takes one argument,
but its type `[a0]' has none
In the expression: 0 : 1 : zipWith (+) fibs $ tail fibs
In an equation for `fibs':
fibs = 0 : 1 : zipWith (+) fibs $ tail fibs
fibonacci.hs:1:16:
Couldn't match expected type `[a0]' with actual type `[a1] -> [a1]'
In the return type of a call of `zipWith'
Probable cause: `zipWith' is applied to too few arguments
In the second argument of `(:)', namely `zipWith (+) fibs'
In the second argument of `(:)', namely `1 : zipWith (+) fibs'
当然,由于 $ 是一个函数,例如:
fibs = 0 : 1 $ zipWith (+) fibs (tail fibs)
行不通,所以至少我的教授给出的解释过于简单化了。在写这篇文章时,我试图放置括号,以便错误是相同的。我有:
fibs = (0 : 1 : zipWith (+) fibs) $ tail fibs
和
fibs = (0 : 1 : zipWith (+) fibs) (tail fibs)
两者都给了我完全相同的错误消息(当然,列号除外)。为什么是这样?ab $ cd 是否等同于 (ab) (cd) 而不是 ab (cd)?我认为这一切都与函数优先级和/或关联性有关,但我不知道具体细节。我不知道你怎么能看到一个函数的优先级(除了尝试很多组合),我也不能用谷歌找到它。
我希望有人能帮我解决这个问题!