简体   繁体   English

Haskell:如何识别咖喱函数?

[英]Haskell: How to recognize a curried function?

Given the following two functions: 给定以下两个功能:

minCur a = (\b -> if a > b then b else a)

minCur a b = if a > b then b else a

Asking for the type results for both the same: 要求两种类型的结果相同:

minCur :: Ord p => p -> p -> p

How does the User of the function know if he is dealing with a curried function? 函数的用户如何知道他是否正在处理咖喱函数? Does it even matter? 有关系吗

Edit: 编辑:

Is this currying? 这是在招惹吗?

minCur (a,b) = if a > b then b else a

It does not matter, since every function that takes more than one argument is a curried function. 没关系,因为每个接受多个参数的函数都是咖喱函数。 In Haskell, every function takes a single parameter and returns a single value ( a -> b -> c is the same as a -> (b -> c) ). 在Haskell中,每个函数都带有一个参数并返回一个值( a -> b -> ca -> (b -> c) )。

There shouldn't be a reason for the user to care whether the function is curried or not. 用户不必理会该功能是否已被咖喱化。

The two function definitions you provided are considered to be the same function. 您提供的两个函数定义被视为同一函数。 The user shouldn't need to care beyond what the behavior of minCur is and what type it has. 用户不必关心minCur的行为和类型。

In the case of the version with the tuple, it has a different type. 对于带有元组的版本,它具有不同的类型。 So 所以

 min (a, b) = if a > b then b else a
 min :: Ord p => (p, p) -> p

And the types (a, a) -> a and a -> a -> a are incompatible. 并且类型(a, a) -> a和a- a -> a -> a a- a -> a -> a不兼容。 You cannot curry the function with the tuple because it only has one parameter anyway and does not return a function type. 您不能使用元组来使用该函数,因为它无论如何都只有一个参数,并且不返回任何函数类型。

In the libraries, we have a function curry which transforms 在库中,我们有一个函数curry可以转换

f :: (A,B) -> C
f (a,b) = ...

into g = curry f , defined as 变成g = curry f ,定义为

g :: A -> B -> C
g a b = ...

We also have the inverse transformation, called uncurry which maps g back into f = uncurry g . 我们还有一个逆变换,称为uncurry ,它将g映射回f = uncurry g

Sometimes, functions like g are called "curried" and functions like f are called "uncurried". 有时,像g这样的f称为“ curried”,而像f这样的f称为“ uncurried”。 This way of speaking is a bit informal since we should instead say, more properly, " g is the curried form of f " and " f is the uncurried form of g ". 这种说法有点非正式,因为我们应该更恰当地说,“ gf的咖喱形式”和“ fg的未经咖喱形式”。

There is no perfect distinction between "curried" and "uncurried" functions on ther own. 单独的“ curried”和“ uncurried”功能之间没有完美的区别。 For instance, h :: (A,B) -> C -> D can be seen as the currying of h1 :: ((A,B),C) -> D or the uncurrying of h2 :: A -> B -> C -> D , so it would be both curried and uncurred at the same time. 例如, h :: (A,B) -> C -> D可以看成是h1 :: ((A,B),C) -> Dh2 :: A -> B -> C -> D ,因此将同时对其进行咖喱化处理。

Often, though, we say that a function is curried or not depending on whether it takes a tuple as a single argument or its components as separate arguments. 但是,通常我们说一个函数是否被咖喱依赖于它是将元组作为单个参数,还是将其组件作为单独的参数。 Again, this is a bit informal. 同样,这有点非正式。

When choosing which style to follow, in Haskell we prefer the curried form since it is easier to apply partially. 在选择要遵循的样式时,在Haskell中,我们更喜欢使用咖喱形式,因为它更易于部分应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM