关于Haskell中的($)的混淆:示例其中($)不明显替换括号

时间:2015-09-13 14:29:37

标签: haskell

假设我有数据类型

data Price = Price Double
data Book = Book {title :: String, bookPrice :: Price}

用函数提取数值

priceAsDouble :: Price -> Double
priceAsDouble (Price doubleValue) = doubleValue

现在,我想在图书清单上写一个累加器,例如

go :: Double -> Book -> Double
go acc book = acc + priceAsDouble (bookPrice book)

这很好并且编译。

但是,如果我将最后一行更改为

go acc book = acc + priceAsDouble $ bookPrice book

我得到以下矛盾的编译器错误:

<interactive>:10:51:
Couldn't match expected type ‘Price -> Double’
            with actual type ‘Double’
The first argument of ($) takes one argument,
but its type ‘Double’ has none
In the expression: acc + priceAsDouble $ bookPrice book
In an equation for ‘go’:
    go acc book = acc + priceAsDouble $ bookPrice book

<interactive>:10:57:
Couldn't match expected type ‘Double’
            with actual type ‘Price -> Double’
Probable cause: ‘priceAsDouble’ is applied to too few arguments
In the second argument of ‘(+)’, namely ‘priceAsDouble’
In the expression: acc + priceAsDouble

问题:我认为($)只不过是圆括号()的语法糖。显然,我错了。我的想法中的错误在哪里?

0 个答案:

没有答案