Lisp映射函数MAP,MAPC, MAPCAR, MAPCAN, MAPL, MAPLIST, MAPCON对比

本文介绍了Lisp中的映射函数,包括MAP及MAPC, MAPCAR, MAPCAN, MAPL, MAPLIST, MAPCON等,它们在List上的不同操作。MAP函数适用于各种序列类型,而MAPC, MAPCAR等专注于List。这些函数根据结果类型和返回值的不同,提供了对List进行遍历和处理的多种方式,如累积结果、连接结果列表等。" 53257766,1343600,JavaScript实现跨浏览器的Class类获取,"['JavaScript', '浏览器兼容', 'DOM操作']
摘要由CSDN通过智能技术生成

介绍之前有必要先介绍一个Lisp的集合数据类型,这样才能更容易的理解这几个的函数的区别和联系。任何语言对集合的分类可以分为两种类型,一种是以一个整数为索引的类数组类型 和 一个可以用key来访问值的表类型。在不同的语言中,前者一般叫做数组(array),列表(list)和元组(tuples)。后者一般叫做哈希表(hash table),关联数组 (associative arrays),  Map(maps),字典(dictonaries).

Lisp是以list起家的语言,所以列表广为所知。其实lisp包含了 array,vector,和list已经hash table等多种数据类型。Lisp把 array ,vector 和list统称为sequence (序列)。也就是集合分类的第一种类型。 如果区分更细致的话,其实array应该属于vector的子类型,但是一般没有必要。另外string类型也是属于序列(sequence),因为string可以看做字符的array。sequence抽象类型之上的操作对string,array,vector和list都是通用的。sequence的各个分类也有其特殊的操作。Lisp的List应为太灵活了,因此好多数据结构都可以使用List实现,例如alist plist就是在list之上实现类似 hash table的功能,导致List是Lisp中使用最广的数据类型。 理所当然List的操作函数也是相当的丰富。MAPC, MAPCAR, MAPCAN, MAPL, MAPLIST, MAPCON都是在List之上的操作函数。
 
于是乎引出了本文,先说MAP. MAP函数操作的对象是Sequence,所以map可以应用在vector string list等sequence的子类型。因为Map在Sequence抽象数据类型之上操作,所以必须指定Map的返回类型,这就是map函数的第一个参数。其他的MAPC, MAPCAR, MAPCAN, MAPL, MAPLIST, MAPCON在List之上的操作函数。

下面我来具体介绍各个函数的说明:首先是Map

Function MAPSyntax:
map result-type function &rest sequences+ => result
Arguments and Values:
result-type -- a sequence type specifier, or nil.
function---a function designator. function must take as many arguments as there are sequences.
sequence---a proper sequence.
result---if result-type is a type specifier other than nil, then a sequence of the type it denotes; otherwise (if the result-type is nil), nil.
Description:
Applies function to successive sets of arguments in which one argument is obtained from each sequence. The function is called first on all the elements with index 0, then on all those with index 1, and so on. The result-type s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值