[英]What does `… -> t a ->…` mean in type signature in Haskell?
In the sign signature of the functions all
and any
, I see this: 在函数all
和any
签名中,我看到:
Prelude> :t all
all :: Foldable t => (a -> Bool) -> t a -> Bool
Prelude> :t any
any :: Foldable t => (a -> Bool) -> t a -> Bool
However, what does ... -> ta ->...
mean ? 但是, ... -> ta ->...
是什么意思?
Isn't both t
and a
a type variable ? 是不是都t
和a
类型变量? if so, what does the juxtapositions of two type variable mean in Haskell ? 如果是这样,两个类型变量的并置在Haskell中意味着什么?
Isn't both t and aa type variable? 是不是t和aa类型变量? if so, what does the juxtapositions of two type variable mean in Haskell? 如果是这样,两个类型变量的并置在Haskell中意味着什么?
Like a data constructor can take parameters (for example Just
), a type constructor can take parameters as well, these parameters are then types. 就像数据构造函数可以获取参数 (例如Just
)一样, 类型构造函数也可以获取参数,这些参数就是类型。 For example the Maybe
type constructor takes a type parameter (for example Int
), and then is a type Maybe Int
. 例如, Maybe
类型构造函数采用类型参数(例如Int
),然后是类型Maybe Int
。
One can for example use a list []
to store elements, but there is still a type parameter to be resolved: the type of objects the list will store. 例如,可以使用list []
来存储元素,但仍有一个要解析的类型参数:列表将存储的对象类型。 [] Int
or [Int]
is then a list of Int
s. [] Int
或[Int]
则是Int
的列表。
Now Foldable
types are types that can be folded. 现在, Foldable
类型是可以折叠的类型。 For example a list []
is a Foldable
(note that we use []
, not [a]
or [Int]
) as well as a Maybe
, or for example a Tree
. 例如,list []
是Foldable
(注意我们使用[]
,而不是[a]
或[Int]
)以及Maybe
,或者例如Tree
。 Most "collections" of elements are Foldable
s, although types that are not collections can be Foldable
s and vice versa. 元素的大多数“集合”都是Foldable
的,尽管不是集合的类型可以是Foldable
的,反之亦然。
The all :: Foldable t => (a -> Bool) -> ta -> Bool
and any :: Foldable t => (a -> Bool) -> ta -> Bool
are functions that can operate on Foldable
s. all :: Foldable t => (a -> Bool) -> ta -> Bool
和any :: Foldable t => (a -> Bool) -> ta -> Bool
是可以在Foldable
运行的函数。 The advantage is thus that we can write all
and any
to operate on a list of a
s (so [a]
), but a Maybe a
can be used as well: you can see Maybe
as a type that is a collection that either contains no elements ( Nothing
), or exactly one element ( Just x
with x
the element). 其优点是这样,我们可以写all
和any
对清单上运行a
S(所以[a]
但Maybe a
可以作为得好:你可以看到Maybe
作为一个类型,它是一个集合,要么包含没有元素( Nothing
),或者只有一个元素( Just x
with x
元素)。 We can for example write: 我们可以写例如:
Prelude> all id Nothing
True
Prelude> all id (Just True)
True
Prelude> all id (Just False)
False
Prelude> all id []
True
Prelude> all id [True]
True
Prelude> all id [False]
False
Prelude> all id [False, True]
False
Prelude> all id [True, True]
True
The all
thus can work on lists, Maybe
s, but a lot of other types as well. 因此, all
这些all
可以在列表上运行, Maybe
是s,但也有很多其他类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.