📅  最后修改于: 2020-11-03 07:14:37             🧑  作者: Mango
序列是LISP中的抽象数据类型。向量和列表是此数据类型的两个具体子类型。序列数据类型上定义的所有功能实际上都应用于所有向量和列表类型。
在本节中,我们将讨论序列上最常用的函数。
在开始各种操作序列(即向量和列表)的方法之前,让我们看一下所有可用函数的列表。
函数make-sequence允许您创建任何类型的序列。该函数的语法是-
make-sequence sqtype sqsize &key :initial-element
它创建一个类型为sqtype和长度为sqsize的序列。
您可以选择使用:initial-element参数指定一些值,然后将每个元素初始化为该值。
例如,创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(write (make-sequence '(vector float)
10
:initial-element 1.0))
当您执行代码时,它返回以下结果-
#(1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0)
Sr.No. | Function & Description |
---|---|
1 |
elt It allows access to individual elements through an integer index. |
2 |
length It returns the length of a sequence. |
3 |
subseq It returns a sub-sequence by extracting the subsequence starting at a particular index and continuing to a particular ending index or the end of the sequence. |
4 |
copy-seq It returns a sequence that contains the same elements as its argument. |
5 |
fill It is used to set multiple elements of a sequence to a single value. |
6 |
replace It takes two sequences and the first argument sequence is destructively modified by copying successive elements into it from the second argument sequence. |
7 |
count It takes an item and a sequence and returns the number of times the item appears in the sequence. |
8 |
reverse It returns a sequence contains the same elements of the argument but in reverse order. |
9 |
nreverse It returns the same sequence containing the same elements as sequence but in reverse order. |
10 |
concatenate It creates a new sequence containing the concatenation of any number of sequences. |
11 |
position It takes an item and a sequence and returns the index of the item in the sequence or nil. |
12 |
find It takes an item and a sequence. It finds the item in the sequence and returns it, if not found then it returns nil. |
13 |
sort It takes a sequence and a two-argument predicate and returns a sorted version of the sequence. |
14 |
merge It takes two sequences and a predicate and returns a sequence produced by merging the two sequences, according to the predicate. |
15 |
map It takes an n-argument function and n sequences and returns a new sequence containing the result of applying the function to subsequent elements of the sequences. |
16 |
some It takes a predicate as an argument and iterates over the argument sequence, and returns the first non-NIL value returned by the predicate or returns false if the predicate is never satisfied. |
17 |
every It takes a predicate as an argument and iterate over the argument sequence, it terminates, returning false, as soon as the predicate fails. If the predicate is always satisfied, it returns true. |
18 |
notany It takes a predicate as an argument and iterate over the argument sequence, and returns false as soon as the predicate is satisfied or true if it never is. |
19 |
notevery It takes a predicate as an argument and iterate over the argument sequence, and returns true as soon as the predicate fails or false if the predicate is always satisfied. |
20 |
reduce It maps over a single sequence, applying a two-argument function first to the first two elements of the sequence and then to the value returned by the function and subsequent elements of the sequence. |
21 |
search It searches a sequence to locate one or more elements satisfying some test. |
22 |
remove It takes an item and a sequence and returns the sequence with instances of item removed. |
23 |
delete This also takes an item and a sequence and returns a sequence of the same kind as the argument sequence that has the same elements except the item. |
24 |
substitute It takes a new item, an existing item, and a sequence and returns a sequence with instances of the existing item replaced with the new item. |
25 |
nsubstitute It takes a new item, an existing item, and a sequence and returns the same sequence with instances of the existing item replaced with the new item. |
26 |
mismatch It takes two sequences and returns the index of the first pair of mismatched elements. |
Argument | Meaning | Default Value |
---|---|---|
:test | It is a two-argument function used to compare item (or value extracted by :key function) to element. | EQL |
:key | One-argument function to extract key value from actual sequence element. NIL means use element as is. | NIL |
:start | Starting index (inclusive) of subsequence. | 0 |
:end | Ending index (exclusive) of subsequence. NIL indicates end of sequence. | NIL |
:from-end | If true, the sequence will be traversed in reverse order, from end to start. | NIL |
:count | Number indicating the number of elements to remove or substitute or NIL to indicate all (REMOVE and SUBSTITUTE only). | NIL |
我们刚刚讨论了用作序列的这些函数中的参数的各种函数和关键字。在下一部分中,我们将通过示例来了解如何使用这些功能。
length函数返回序列的长度,而elt函数允许您使用整数索引访问单个元素。
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(setq x (vector 'a 'b 'c 'd 'e))
(write (length x))
(terpri)
(write (elt x 3))
当您执行代码时,它返回以下结果-
5
D
一些序列函数允许遍历序列并执行某些操作,例如搜索,删除,计数或过滤特定元素,而无需编写显式循环。
以下示例演示了这一点-
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(write (count 7 '(1 5 6 7 8 9 2 7 3 4 5)))
(terpri)
(write (remove 5 '(1 5 6 7 8 9 2 7 3 4 5)))
(terpri)
(write (delete 5 '(1 5 6 7 8 9 2 7 3 4 5)))
(terpri)
(write (substitute 10 7 '(1 5 6 7 8 9 2 7 3 4 5)))
(terpri)
(write (find 7 '(1 5 6 7 8 9 2 7 3 4 5)))
(terpri)
(write (position 5 '(1 5 6 7 8 9 2 7 3 4 5)))
当您执行代码时,它返回以下结果-
2
(1 6 7 8 9 2 7 3 4)
(1 6 7 8 9 2 7 3 4)
(1 5 6 10 8 9 2 10 3 4 5)
7
1
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(write (delete-if #'oddp '(1 5 6 7 8 9 2 7 3 4 5)))
(terpri)
(write (delete-if #'evenp '(1 5 6 7 8 9 2 7 3 4 5)))
(terpri)
(write (remove-if #'evenp '(1 5 6 7 8 9 2 7 3 4 5) :count 1 :from-end t))
(terpri)
(setq x (vector 'a 'b 'c 'd 'e 'f 'g))
(fill x 'p :start 1 :end 4)
(write x)
当您执行代码时,它返回以下结果-
(6 8 2 4)
(1 5 7 9 7 3 5)
(1 5 6 7 8 9 2 7 3 5)
#(A P P P E F G)
排序功能采用一个序列和一个两个参数的谓词,并返回该序列的排序版本。
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(write (sort '(2 4 7 3 9 1 5 4 6 3 8) #'))
(terpri)
当您执行代码时,它返回以下结果-
(1 2 3 3 4 4 5 6 7 8 9)
(9 8 7 6 5 4 4 3 3 2 1)
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(write (merge 'vector #(1 3 5) #(2 4 6) #'
当您执行代码时,它返回以下结果-
#(1 2 3 4 5 6)
(1 2 3 4 5 6)
每个函数,每个函数,非所有函数和每个函数都称为序列谓词。
这些函数遍历序列并测试布尔谓词。
所有这些函数都将谓词作为第一个参数,其余参数为序列。
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(write (every #'evenp #(2 4 6 8 10)))
(terpri)
(write (some #'evenp #(2 4 6 8 10 13 14)))
(terpri)
(write (every #'evenp #(2 4 6 8 10 13 14)))
(terpri)
(write (notany #'evenp #(2 4 6 8 10)))
(terpri)
(write (notevery #'evenp #(2 4 6 8 10 13 14)))
(terpri)
当您执行代码时,它返回以下结果-
T
T
NIL
NIL
T
我们已经讨论了映射功能。类似地,映射函数允许您将函数应用于一个或多个序列的后续元素。
map函数采用n参数函数和n个序列,并在将该函数应用于序列的后续元素后返回一个新序列。
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(write (map 'vector #'* #(2 3 4 5) #(3 5 4 8)))
当您执行代码时,它返回以下结果-
#(6 15 16 40)