简体   繁体   English

什么是参数多态 function?

[英]What is a parametrically polymorphic function?

Can any one explain this to me in relation to sorting algorithms?有人可以就排序算法向我解释一下吗?

Let me try the simplest thing I can.让我尝试我能做的最简单的事情。

Suppose you have a pair of integers:假设你有一对整数:

foo :: (Int, Int) 
foo = (2,5)

and suppose you want a function that swap the position of the integers on that pair.并假设您想要一个 function 交换该对上整数的 position。 You could do this:你可以这样做:

swapInt :: (Int, Int) -> (Int, Int)
swapInt (x,y) = (y,x)

But now if you need a similar function for Double s, you'd have to implement it againt:但是现在如果你需要一个类似的 function 用于Double s,你必须再次实现它:

swapDouble :: (Double, Double) -> (Double, Double)
swapDouble (x,y) = (y,x)

You have to note a couple of things: (1) the codes of swapDouble and swapInt are identical except for their type signatures, (2) nowhere in the code you make reference to anything that would depend on what are the types of x and y .您必须注意几件事:(1) swapDoubleswapInt的代码除了它们的类型签名外是相同的,(2)您在代码中没有任何地方引用任何取决于xy类型的内容. This code is valid whatever are their types.无论其类型如何,此代码均有效。 So there should be a way to write the code just once and let the compiler specialize the code automatically for each type you need.所以应该有一种方法可以只编写一次代码,让编译器针对您需要的每种类型自动专门化代码。 The way to do this is parametrical polymorphism.实现这一点的方法是参数多态性。 For this particular case, you can write:对于这种特殊情况,您可以编写:

swap :: (a,b) -> (b,a)
swap (x,y) = (y,x)

What this means?这意味着什么? You're telling the compiler: there's a function swap, that takes a pair (x,y) where x is of type a and y is of type b, and return the pair (y,x).您是在告诉编译器:有一个 function 交换,它采用一对 (x,y),其中 x 是 a 类型,y 是 b 类型,并返回对 (y,x)。 a and b can be any type, thus this function is called a polymorphic function. a 和 b 可以是任何类型,因此这个 function 称为多态 function。 When you apply swap to a specific pair, the compiler will check the type of this pair and automatically instantiate a version of this function that is adequate for your tuple.当您将swap应用于特定对时,编译器将检查该对的类型并自动实例化此 function 的版本,该版本足以满足您的元组。

For example:例如:

swap ('a', 1) = (1,'a')  -- in this case swap :: (Char, Int) -> (Int, Char) 
swap ( 0 , 5) = (5, 0 )  -- in this case swap :: (Int , Int) -> (Int, Int )

Let's understand the name: polymorphic is any function or data structure that works with many different types.让我们理解这个名称:多态是任何 function 或适用于许多不同类型的数据结构。 Parametric cause the way to implement the polymorphism is to have "type parameters" in the type of the function or data structure.参数化实现多态性的方式是在function或数据结构的类型中具有“类型参数”。 When you write (a,b) , a and b are type parameters.当您编写(a,b)时, ab是类型参数。

Many data structures can be implemented irrespective of the type that is contained in then: lists, arrays, maps, tuples,... all of them can have a parametrically polymorphic implementation.无论 then 中包含何种类型,都可以实现许多数据结构:列表、arrays、映射、元组……它们都可以具有参数化的多态实现。 And functions that operate on them: sort, map, fold,... can be implemented without having to refer to specific types, but to type parameters that will be specialized automatically by the compiler.以及对它们进行操作的函数:sort、map、fold、... 可以在不必引用特定类型的情况下实现,但要输入编译器会自动专门化的参数。

Other kinds of polymorphism exist, and Haskell also implement ad hoc polymorphism with typeclasses, for example.还存在其他类型的多态性,例如,Haskell 也使用类型类实现临时多态性。

A function which is agnostic to the argument types it works with.一个 function 与它使用的参数类型无关。

linear_search f n [] = Nothing
linear_search f n (x:xs)
    | f n x     = Just x
    | otherwise = linear_search f n xs

My Haskell is rusty, so if someone could correct mistakes in the comments that would be appreciated.我的 Haskell 生锈了,所以如果有人能纠正评论中的错误,将不胜感激。

The idea here is that linear_search can preform a linear search on a list of any type;这里的想法是linear_search可以对任何类型的列表进行线性搜索; this is what makes the function parametrically (meaning the function parameters) polymorphic (because they can be many types).这就是使 function 参数化(即 function 参数)多态的原因(因为它们可以是多种类型)。

# preforming on integers
linear_search (==) 5 [1,2,3,4,5]
# returns Just 5

# preforming on strings
linear_search (elem) 'e' ["doctor", "apron", "horse", "sky"]
# returns Just "horse"

When talking about the type of this function, it is stated as (a -> b -> Bool) -> a -> [b] -> Maybe b .当谈到这个 function 的类型时,它被表述为(a -> b -> Bool) -> a -> [b] -> Maybe b Importantly, letters indicate type variables , meaning their type can be anything -- again the property which makes functions parametrically polymorphic.重要的是,字母表示类型变量,这意味着它们的类型可以是任何东西——这也是使函数参数多态的属性。

Parametric polymorphism allows a function or a data type to be written generically, so that it can handle values identically without depending on their type.参数多态性允许 function 或数据类型被通用写入,因此它可以相同地处理值而不依赖于它们的类型。 Parametric polymorphism is a way to make a language more expressive, while still maintaining full static type-safety.参数多态性是一种使语言更具表现力的方法,同时仍保持完整的 static 类型安全。

-- from: http://en.wikipedia.org/wiki/Polymorphism_(computer_science ). -- 来自: http://en.wikipedia.org/wiki/Polymorphism_(computer_science )。

In relation to searches, I guess that depends more on the exact context - I can't help there.关于搜索,我想这更多地取决于确切的上下文 - 我无能为力。

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

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