2015-03-03 60 views
1

约从http://www.seas.upenn.edu/~cis194/lectures/02-lists.html咖喱/ uncurry(schönfinkel/unschönfinkel)的定义,如何理解咖喱的函数定义/ uncurry

schönfinkel :: ((a,b) -> c) -> a -> b -> c 
schönfinkel f x y = f (x,y) 

unschönfinkel :: (a -> b -> c) -> (a,b) -> c 
unschönfinkel f (x,y) = f x y 

但我觉得上面这些函数的定义应该是:

schönfinkel :: ((a,b) -> c) -> a -> b -> c 
schönfinkel f (x,y) = f x y 
-- schönfinkel(curry) converts an uncurried function 
-- (f (x,y), its type signature is (a,b) -> c) 
-- to a curried function 
-- (f x y, its type signature is a -> b -> c) 

unschönfinkel :: (a -> b -> c) -> (a,b) -> c 
unschönfinkel f x y = f (x,y) 
-- unschönfinkel(uncurry) converts a curried function 
-- (f x y , its type signature is a -> b -> c) 
-- to an uncurried function 
-- (f (x,y), its type signature is (a,b) -> c) 

请有人给我一个简单的解释?

+0

在'schönfinkel'中,如果'f'具有类型'(a,b) - > c'then然后你不能像你所做的那样将它应用于2个参数。你在一个元组上匹配模式,但是类型表示还有两个参数,它们都不是元组。 – user2407038 2015-03-03 05:40:56

回答

4

你可能误解/误解的初始代码,一个适当的缩进大概是足以得到它的权利:

schönfinkel :: ((a,b) -> c) -> a -> b -> c 
schönfinkel f    x y = f (x,y) 

unschönfinkel :: (a -> b -> c) -> (a,b) -> c 
unschönfinkel f    (x,y) = f x y 

现在,让我们打开ghci中和尝试了几件事:

>>> let schönfinkel f x y = f (x,y) 

>>> let toBeCurried (x,y) = x ++ y 
>>> :t toBeCurried 
toBeCurried :: ([a], [a]) -> [a] 

>>> :t schönfinkel toBeCurried 
schönfinkel toBeCurried :: [a] -> [a] -> [a] 

看看你给的非正式定义,你会看到它匹配e行为schönfinkel

+0

请确认我的理解是否正确: f(x,y)(schönfinkel的函数体)并不意味着它是一个函数,它只是元组(x,y)中f的一个VAULE。我对吗? – 2015-03-03 11:03:25

+0

在右边的部分,'f(x,y)'是参数中给出的函数'f'的应用,由'schöfinkel'的最后两个参数构成的元组''(x,y)''。根据'f'的签名,'f(x,y)'被解释为类型'c'的“值”。 – Nicolas 2015-03-03 11:15:59

-5

这里是由Dierk &钴

的基本思想是利用一个函数具有多个参数 和通过固定它的一些转换成具有较少参数的函数的一小块的约在Groovy在行动咖喱解释价值。一个典型的例子是选择一些任意值n并将一个将两个参数相加的函数转换为一个函数,该函数只接受一个参数并将n加入到函数中。

Groovy中有一个内置的闭合的方法称为咖喱这需要任意值绑定到闭包的参数和约束值的各个参数后返回封闭的克隆。请参考下面的链接,详细了解

Groovy functional programming