2013-07-08 55 views
9

我正在學習haskell,並且有點困惑,函數應用操作符$ curry的方式如何。

根據GHC的$類型是

*Main>:t ($) 
($) :: (a->b) -> a -> b 

但我可以鍵入下面的代碼

*Main>map ($ 2) [(*2), (+2), (/2)] 
[4.0,4.0,1.0] 

據$簽名雖然我認爲我會需要使用翻轉函數,因爲$的第一個參數是(a-> b)。

舉例來說,我不能做以下

curry_test :: Integer -> String -> String 
curry_test x y = (show x) ++ " " ++ y 
*Main> let x = curry_test "123" 
    Couldn't match expected type `Integer' with actual type `[Char]' 
In the first argument of `curry_test', namely `"123"' 
In the expression: curry_test "123" 
In an equation for `x': x = curry_test "123" 

但我可以做

let x = curry_test 2 
+0

提示:'(*)::民一=> A - > A - >了' – DiegoNolan

回答

11

綴運算符有特殊的規則。看到這個頁面:http://www.haskell.org/haskellwiki/Section_of_an_infix_operator

基本上,因爲$是中綴操作符,($ 2)真的修補2作爲$第二個參數,所以它相當於flip ($) 2

這個想法是讓操作員更直觀地進行部分應用,例如,如果map (/ 2)在列表上,您可以想象將列表中的每個元素放在劃分符號左側的「缺失」點。

如果你想用你的curry_test功能這種方式,你可以做

let x = (`curry_test` "123") 
相關問題