LISP 中的列表操作
在本文中,我们将讨论 LISP 中的列表操作。列表是一种数据结构,可以存储多种数据类型的元素
我们可以使用 list函数创建一个列表
语法:
(list element1 element2 .................. element n)
列表操作函数用于操作和返回元素列表
1. car()函数:
该函数用于显示列表中的第一个元素
语法:
(car '(element1 element2 ........... element n))
例子:
Lisp
;list of integer elements
(write (car '(1 2 3 4 5)))
(terpri)
;list of characters
(write (car '(a b)))
(terpri)
Lisp
;list of integer elements
(write (cdr '(1 2 3 4 5)))
(terpri)
;list of characters
(write (cdr '(a b)))
(terpri)
Lisp
;list of integer elements
;insert 100
(write (cons '100 '(1 2 3 4 5)))
(terpri)
;list of characters
;insert a
(write (cons 'a '(b c d e f)))
(terpri)
Lisp
;two lists and one variable
(write (list '100 '(1 2 3 4 5) '(g e e k s)))
(terpri)
Lisp
;consider two lists and append
(write (append '(1 2 3 4 5) '(g e e k s)))
(terpri)
;consider three lists and append
(write (append '(10 20) '(20 30 45) '(a s d)))
(terpri)
Lisp
;consider a list and get last element
(write (last '(1 2 3 4 5) ))
(terpri)
;consider a list and get last element
(write (last '(10 20 ( g e e k s))))
(terpri)
Lisp
;consider a list with 1 as member
(write (member '1 '(1 2 3 4 5) ))
(terpri)
;consider a list with 10 as member
(write (member '10 '(1 2 3 4 5) ))
(terpri)
Lisp
;consider a list and reverse
(write (reverse '(1 2 3 4 5) ))
(terpri)
;consider a list nnd reverse
(write (reverse '(g e e k s) ))
(terpri)
输出:
1
A
2. cdr()函数:
该函数用于显示除第一个元素之外的所有元素的列表
语法:
(cdr '(element1 element2 ........... element n))
例子:
语言
;list of integer elements
(write (cdr '(1 2 3 4 5)))
(terpri)
;list of characters
(write (cdr '(a b)))
(terpri)
输出:
(2 3 4 5)
(B)
3.cons()函数:
此函数将采用两项。一个是值,另一个是列表。这会将值插入列表中的第一位并返回新列表
语法:
(cons 'value '(list_of_elements)))
在哪里,
- value是要插入的元素
- list_of_elements是元素列表
例子:
语言
;list of integer elements
;insert 100
(write (cons '100 '(1 2 3 4 5)))
(terpri)
;list of characters
;insert a
(write (cons 'a '(b c d e f)))
(terpri)
输出:
(100 1 2 3 4 5)
(A B C D E F)
4. list()函数:
此函数将获取多个列表并显示它
语法:
(list 'list1 'list2 ........ 'listn)
例子:
语言
;two lists and one variable
(write (list '100 '(1 2 3 4 5) '(g e e k s)))
(terpri)
输出:
(100 (1 2 3 4 5) (G E E K S))
5. append()函数:
此函数将附加两个或多个列表。
句法:
(append'list1 'list2 ........ 'listn)
例子:
语言
;consider two lists and append
(write (append '(1 2 3 4 5) '(g e e k s)))
(terpri)
;consider three lists and append
(write (append '(10 20) '(20 30 45) '(a s d)))
(terpri)
输出:
(1 2 3 4 5 G E E K S)
(10 20 20 30 45 A S D)
6. last()函数:
此函数返回包含列表中最后一个元素的列表
语法:
(last 'list)
示例:
语言
;consider a list and get last element
(write (last '(1 2 3 4 5) ))
(terpri)
;consider a list and get last element
(write (last '(10 20 ( g e e k s))))
(terpri)
输出:
(5)
((G E E K S))
成员()函数:
此函数用于根据给定的成员返回列表。
它需要两个参数:
- 第一个参数将是值。
- 第二个参数将是列表。
如果该值存在于列表中,则它将返回列表
否则,返回 NIL。
语法:
(member 'value 'list )
例子:
语言
;consider a list with 1 as member
(write (member '1 '(1 2 3 4 5) ))
(terpri)
;consider a list with 10 as member
(write (member '10 '(1 2 3 4 5) ))
(terpri)
输出:
(1 2 3 4 5)
NIL
8. reverse()函数:
此函数反转列表并返回反转的列表
句法:
(reverse 'list)
例子:
语言
;consider a list and reverse
(write (reverse '(1 2 3 4 5) ))
(terpri)
;consider a list nnd reverse
(write (reverse '(g e e k s) ))
(terpri)
输出:
(5 4 3 2 1)
(S K E E G)