Skip to content

SRFI-1: List 库

SRFI-1 提供了全面的列表操作函数。

导入

scheme
(import (srfi srfi-1))
; 或
(import (liii list))  ; Goldfish 扩展版本

构造函数

函数描述示例
cons构造点对(cons 1 '(2 3)) => (1 2 3)
list构造列表(list 1 2 3) => (1 2 3)
xcons反向 cons(xcons '(a b) 'c) => (c a b)
cons*构造 Improper List(cons* 1 2 '(3 4)) => (1 2 3 4)
make-list创建列表(make-list 5 'x) => (x x x x x)
list-tabulate按索引创建(list-tabulate 5 (lambda (i) (* i i))) => (0 1 4 9 16)
list-copy复制列表(list-copy '(1 2 3)) => (1 2 3)
circular-list循环列表(circular-list 1 2) => #(1 2 1 2 ...)
iota整数序列(iota 5) => (0 1 2 3 4)

谓词

函数描述
pair?是否为点对
null?是否为空列表
proper-list?是否为 proper list
circular-list?是否为循环列表
dotted-list?是否为 dotted list
null-list?是否为空列表(SRFI-1 版本)
list=列表相等比较

选择器

函数描述示例
car / first第一个元素(first '(1 2 3)) => 1
cadr / second第二个元素(second '(1 2 3)) => 2
caddr / third第三个元素...
.........
tenth第十个元素(tenth '(1 2 ... 10)) => 10
car+cdr同时获取 car 和 cdr(car+cdr '(1 2 3)) => 1 (2 3)
take取前 n 个(take '(1 2 3 4 5) 3) => (1 2 3)
drop丢弃前 n 个(drop '(1 2 3 4 5) 3) => (4 5)
take-right从后取 n 个(take-right '(1 2 3 4 5) 2) => (4 5)
drop-right从后丢弃 n 个(drop-right '(1 2 3 4 5) 2) => (1 2 3)
split-at分割列表(split-at '(1 2 3 4 5) 3) => (1 2 3) (4 5)
last最后一个元素(last '(1 2 3)) => 3
last-pair最后一个点对(last-pair '(1 2 3)) => (3)

更多函数

更多详细文档请参考 (liii list)

基于 Apache 2.0 许可发布