什么是"a"?在“列出一个"在长度示例中? [英] What is the "a" in "List a" in the length example?

查看:101
本文介绍了什么是"a"?在“列出一个"在长度示例中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在哪里可以找到有关长度示例中使用的"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 aList 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并返回bList 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 Stringmap将返回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 Intmap将返回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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆