什么是"a"?在“列出一个"在长度示例中? [英] What is the "a" in "List a" in the length example?
问题描述
我想知道在哪里可以找到有关长度示例中使用的"a
"的信息.似乎是某种类型?
I'm wondering where I can find information about the "a
" used in the Length example. It seems to be a type of some kind?
推荐答案
[1,2,3]
是List Int
,只能与整数列表一起使用的函数的类型签名中必须具有List Int
. ["a", "b"]
是List String
,只能与字符串列表一起使用的函数的类型签名中必须具有List String
.处理任何类型的列表(例如List.length
)的函数都可以具有通用类型签名(例如List a
或List b
). a
的含义仅在类型签名中相关.例如,类型为List a -> a
的函数在给出List Int
时将返回Int
.给定List String
时,它将返回String
.
[1,2,3]
is a List Int
, functions that could only work with a Lists of Ints would have to have List Int
in their type signature. ["a", "b"]
is a List String
, functions that could only work with a Lists of Strings would have to have List String
in their type signature. A function that works with a list of any type (for example List.length
) can have a generic type signature (like List a
or List b
). The meaning of a
is only relevant within the type signature. For example a function with a type of List a -> a
, when given a List Int
would return an Int
. When given a List String
it would return a String
.
例如, map
函数,其签名为(a -> b) -> List a -> List b
.它说给定一个接受a
并返回b
和List a
的函数,它将返回List b
.
Take for example the map
function which has a signature of (a -> b) -> List a -> List b
. It says that given a function that takes an a
and returns a b
, and a List a
, it will return a List b
.
给出一个接受String
并返回Int
的函数,而List String
和map
将返回List Int
.
Given a function that takes a String
and returns an Int
, and a List String
, map
will return a List Int
.
List.map String.length ["a", "aa", "aaa"]
-- [1, 2, 3] : List Int
给出一个接受Int
并返回String
的函数,而List Int
和map
将返回List String
.
Given a function that takes an Int
and returns a String
, and a List Int
, map
will return a List String
.
List.map (\n -> String.repeat n "a") [1, 2, 3]
-- ["a", "aa", "aaa"] : List String
这篇关于什么是"a"?在“列出一个"在长度示例中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!