介绍之前有必要先介绍一个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