Haskell 如何理解curry/uncurry的函数定义
关于咖喱/非咖喱(schönfinkel/unschönfinkel)的定义来自 但我认为上述函数定义应该是:Haskell 如何理解curry/uncurry的函数定义,haskell,Haskell,关于咖喱/非咖喱(schönfinkel/unschönfinkel)的定义来自 但我认为上述函数定义应该是: 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
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)
有人能给我一个简单的解释吗?这里是Dierk&Co.的Groovy In Action关于咖喱的一个小解释 其基本思想是采用具有多个参数的函数 并通过固定一些值将其转换为参数较少的函数。一个经典的例子是选择任意值n,并将两个参数求和的函数转换为接受单个参数并向其中添加n的函数 Groovy有一个内置的闭包方法,名为curry,它接受任意值绑定到闭包参数,并在将值绑定到各个参数后返回闭包克隆。请参考以下链接以了解详细信息
您可能误读/误解了初始代码,适当的缩进可能足以使其正确:
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]
看看你给出的非正规定义,你会发现它与
schönfinkel
中schönfinkel的行为相匹配,如果f
具有类型(a,b)->c
,那么你就不能像以前那样将它应用于两个参数。你在一个元组上进行模式匹配,但是类型说还有两个参数,它们都不是元组。请确认我的理解是否正确:f(x,y)(schönfinkel的函数体)并不意味着它是一个函数,它只是一个带元组(x,y)的f值。我说的对吗?在右边,f(x,y)
是参数中给出的函数f
对元组(x,y)
的应用,该元组由schöfinkel
的最后两个参数组成。根据f
的签名,f(x,y)
被解释为c
类型的“值”。
>>> 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]