On this page:
4.10.1 Pair Constructors and Selectors
pair?
null?
cons
car
cdr
null
list?
list
list*
build-list
4.10.2 List Operations
length
list-ref
list-tail
append
reverse
4.10.3 List Iteration
map
andmap
ormap
for-each
foldl
foldr
4.10.4 List Filtering
filter
remove
remq
remv
remw
remove*
remq*
remv*
remw*
sort
4.10.5 List Searching
member
memw
memv
memq
memf
findf
assoc
assw
assv
assq
assf
4.10.6 Pair Accessor Shorthands
caar
cadr
cdar
cddr
caaar
caadr
cadar
caddr
cdaar
cdadr
cddar
cdddr
caaaar
caaadr
caadar
caaddr
cadaar
cadadr
caddar
cadddr
cdaaar
cdaadr
cdadar
cdaddr
cddaar
cddadr
cdddar
cddddr
4.10.7 Additional List Functions and Synonyms
empty
cons?
empty?
first
rest
second
third
fourth
fifth
sixth
seventh
eighth
ninth
tenth
last
last-pair
make-list
list-update
list-set
index-of
index-where
indexes-of
indexes-where
take
drop
split-at
takef
dropf
splitf-at
take-right
drop-right
split-at-right
takef-right
dropf-right
splitf-at-right
list-prefix?
take-common-prefix
drop-common-prefix
split-common-prefix
add-between
append*
flatten
check-duplicates
remove-duplicates
filter-map
count
partition
range
inclusive-range
append-map
filter-not
shuffle
combinations
in-combinations
permutations
in-permutations
argmin
argmax
group-by
cartesian-product
remf
remf*
4.10.8 Immutable Cyclic Data
make-reader-graph
placeholder?
make-placeholder
placeholder-set!
placeholder-get
hash-placeholder?
make-hash-placeholder
make-hasheq-placeholder
make-hasheqv-placeholder
make-hashalw-placeholder

4.10 Pairs and Lists🔗

+(part ("(lib scribblings/guide/guide.scrbl)" "pairs")) in (part ("(lib scribblings/guide/guide.scrbl)" "top")) introduces pairs and lists.

一个 pair(对)恰好组合了两个值。第一个值通过 car 过程访问,第二个值通过 cdr 过程访问。pair 是不可变的(但参见 Mutable Pairs and Lists)。

list(列表)是递归定义的:它要么是常量 null,要么是一个第二个值为 list 的 pair。

list 可以用作单值序列(参见 Sequences)。list 的元素作为序列的元素使用。另见 in-list

通过 readmake-reader-graph,可以仅使用不可变 pair 创建循环数据结构。如果从一个 pair 开始并使用若干 cdr 后回到起始 pair,则该 pair 不是 list。

See Reading Pairs and Lists for information on reading pairs and lists and Printing Pairs and Lists for information on printing pairs and lists.

4.10.1 Pair Constructors and Selectors🔗

procedure

(pair? v)  boolean?

  v : any/c
如果 v 是 pair 则返回 #t,否则返回 #f

Examples:
> (pair? 1)

#f

> (pair? (cons 1 2))

#t

> (pair? (list 1 2))

#t

> (pair? '(1 2))

#t

> (pair? '())

#f

procedure

(null? v)  boolean?

  v : any/c
如果 v 是空 list 则返回 #t,否则返回 #f

Examples:
> (null? 1)

#f

> (null? '(1 2))

#f

> (null? '())

#t

> (null? (cdr (list 1)))

#t

procedure

(cons a d)  pair?

  a : any/c
  d : any/c
返回一个新分配的 pair,其第一个元素为 a,第二个元素为 d

Examples:
> (cons 1 2)

'(1 . 2)

> (cons 1 '())

'(1)

procedure

(car p)  any/c

  p : pair?
返回 pair p 的第一个元素。

Examples:
> (car '(1 2))

1

> (car (cons 2 3))

2

procedure

(cdr p)  any/c

  p : pair?
返回 pair p 的第二个元素。

Examples:
> (cdr '(1 2))

'(2)

> (cdr '(1))

'()

value

null : null?

空 list。

Examples:
> null

'()

> '()

'()

> (eq? '() null)

#t

procedure

(list? v)  boolean?

  v : any/c
如果 v 是 list 则返回 #t:要么是空 list,要么是一个第二个元素是 list 的 pair。由于内部缓存,该过程实际上是常数时间的(因此任何必要的 pair 遍历原则上可以算作分配 pair 的额外开销)。

Examples:
> (list? '(1 2))

#t

> (list? (cons 1 (cons 2 '())))

#t

> (list? (cons 1 2))

#f

procedure

(list v ...)  list?

  v : any/c
返回一个新分配的 list,包含 v 作为其元素。

Examples:
> (list 1 2 3 4)

'(1 2 3 4)

> (list (list 1 2) (list 3 4))

'((1 2) (3 4))

procedure

(list* v ... tail)  any/c

  v : any/c
  tail : any/c
类似于 list,但最后一个参数用作结果的尾部,而不是最后一个元素。仅当最后一个参数是 list 时,结果才是 list。

Examples:
> (list* 1 2)

'(1 . 2)

> (list* 1 2 (list 3 4))

'(1 2 3 4)

procedure

(build-list n proc)  list?

  n : exact-nonnegative-integer?
  proc : (exact-nonnegative-integer? . -> . any)
通过按顺序对从 0(sub1 n) 的整数应用 proc 来创建一个包含 n 个元素的 list。如果 lst 是结果 list,则 (list-ref lst i)(proc i) 产生的值。

Examples:
> (build-list 10 values)

'(0 1 2 3 4 5 6 7 8 9)

> (build-list 5 (lambda (x) (* x x)))

'(0 1 4 9 16)

4.10.2 List Operations🔗

procedure

(length lst)  exact-nonnegative-integer?

  lst : list?
返回 lst 中元素的数量。此函数的时间与该长度成正比。

Examples:
> (length (list 1 2 3 4))

4

> (length '())

0

procedure

(list-ref lst pos)  any/c

  lst : pair?
  pos : exact-nonnegative-integer?
返回 lst 中位置 pos 处的元素,其中 list 的第一个元素位于位置 0。如果 list 的元素数量不超过 pos,则 exn:fail:contract exception is raised。

lst 参数实际上不必是 list;lst 只需以至少 (add1 pos) 个 pair 的链开始。

此函数的时间与 pos 成正比。

Examples:
> (list-ref (list 'a 'b 'c) 0)

'a

> (list-ref (list 'a 'b 'c) 1)

'b

> (list-ref (list 'a 'b 'c) 2)

'c

> (list-ref (cons 1 2) 0)

1

> (list-ref (cons 1 2) 1)

list-ref: index reaches a non-pair

  index: 1

  in: '(1 . 2)

procedure

(list-tail lst pos)  any/c

  lst : any/c
  pos : exact-nonnegative-integer?
返回 lst 中前 pos 个元素之后的 list。如果 list 的元素少于 pos 个,则 exn:fail:contract exception is raised。

lst 参数实际上不必是 list;lst 只需以至少 pos 个 pair 的链开始。

此函数的时间与 pos 成正比。

Examples:
> (list-tail (list 1 2 3 4 5) 2)

'(3 4 5)

> (list-tail (cons 1 2) 1)

2

> (list-tail (cons 1 2) 2)

list-tail: index reaches a non-pair

  index: 2

  in: '(1 . 2)

> (list-tail 'not-a-pair 0)

'not-a-pair

procedure

(append lst ...)  list?

  lst : list?
(append lst ... v)  any/c
  lst : list?
  v : any/c
当所有参数都是 list 时,结果是一个按顺序包含所有给定 list 中元素的 list。最后一个参数直接用于结果的尾部。

最后一个参数不必是 list,在这种情况下结果是“非正规 list”(improper list)。

此函数的时间与除最后一个参数外所有参数的长度总和成正比。

Examples:
> (append (list 1 2) (list 3 4))

'(1 2 3 4)

> (append (list 1 2) (list 3 4) (list 5 6) (list 7 8))

'(1 2 3 4 5 6 7 8)

procedure

(reverse lst)  list?

  lst : list?
返回一个与 lst 具有相同元素但顺序相反的 list。

此函数的时间与 lst 的长度成正比。

Example:
> (reverse (list 1 2 3 4))

'(4 3 2 1)

4.10.3 List Iteration🔗

procedure

(map proc lst ...+)  list?

  proc : procedure?
  lst : list?
lst 的元素从第一个到最后一个依次应用 procproc 参数必须接受与提供的 lst 数量相同的参数,且所有 lst 必须具有相同数量的元素。结果是一个按顺序包含 proc 每个结果的 list。

Examples:
> (map (lambda (number)
         (+ 1 number))
       '(1 2 3 4))

'(2 3 4 5)

> (map (lambda (number1 number2)
         (+ number1 number2))
       '(1 2 3 4)
       '(10 100 1000 10000))

'(11 102 1003 10004)

procedure

(andmap proc lst ...+)  any

  proc : procedure?
  lst : list?
类似于 map,即 proc 被应用于 lst 的每个元素,但

andmap 函数实际上更接近 foldl 而非 map,因为 andmap 不产生 list。不过,(andmap f (list x y z)) 等价于 (and (f x) (f y) (f z)),其方式与 (map f (list x y z)) 等价于 (list (f x) (f y) (f z)) 相同。

  • 如果 proc 的任何一次应用产生 #f,则结果为 #f,在这种情况下 proc 不会被应用于 lst 的后续元素;并且

  • 结果是 proc 应用于 lst 最后一组元素的值;更具体地说,proc 应用于 lst 最后一组元素相对于 andmap 调用处于尾部位置。

如果 lst 为空,则返回 #t

Examples:
> (andmap positive? '(1 2 3))

#t

> (andmap positive? '(1 2 a))

positive?: contract violation

  expected: real?

  given: 'a

> (andmap positive? '(1 -2 a))

#f

> (andmap + '(1 2 3) '(4 5 6))

9

procedure

(ormap proc lst ...+)  any

  proc : procedure?
  lst : list?
类似于 map,即 proc 被应用于 lst 的每个元素,但

继续上面 andmap 的注释,(ormap f (list x y z)) 等价于 (or (f x) (f y) (f z))

  • 如果 proc 的每次应用都产生 #f,则结果为 #f;并且

  • 结果是 proc 第一次产生非 #f 值的应用结果,在这种情况下 proc 不会被应用于 lst 的后续元素;proc 应用于 lst 最后一组元素相对于 ormap 调用处于尾部位置。

如果 lst 为空,则返回 #f

Examples:
> (ormap eq? '(a b c) '(a b c))

#t

> (ormap positive? '(1 2 a))

#t

> (ormap + '(1 2 3) '(4 5 6))

5

procedure

(for-each proc lst ...+)  void?

  proc : procedure?
  lst : list?
类似于 map,但 proc 仅为其副作用而被调用,其结果(可以是任意数量的值)被忽略。

Example:
> (for-each (lambda (arg)
              (printf "Got ~a\n" arg)
              23)
            '(1 2 3 4))

Got 1

Got 2

Got 3

Got 4

procedure

(foldl proc init lst ...+)  any/c

  proc : procedure?
  init : any/c
  lst : list?
类似于 mapfoldl 将一个过程应用于一个或多个 list 的元素。map 将返回值组合成一个 list,而 foldlproc 决定的任意方式组合返回值。

如果使用 n 个 list 调用 foldl,则 proc 必须接受 n+1 个参数。额外的参数是到目前为止的组合返回值。proc 最初以每个 list 的第一个元素调用,最后一个参数为 init。在后续调用中,最后一个参数是 proc 前一次调用的返回值。输入 lst 从左到右遍历,整个 foldl 应用的结果是 proc 最后一次应用的结果。如果 lst 为空,结果为 init

foldr 不同,foldl 在常数空间内处理 lst(加上每次调用 proc 的空间)。

Examples:
> (foldl cons '() '(1 2 3 4))

'(4 3 2 1)

> (foldl + 0 '(1 2 3 4))

10

> (foldl (lambda (a b result)
           (* result (- a b)))
         1
         '(1 2 3)
         '(4 5 6))

-27

procedure

(foldr proc init lst ...+)  any/c

  proc : procedure?
  init : any/c
  lst : list?
类似于 foldl,但 list 从右到左遍历。与 foldl 不同,foldr 处理 lst 的空间与 lst 的长度成正比(加上每次调用 proc 的空间)。

Examples:
> (foldr cons '() '(1 2 3 4))

'(1 2 3 4)

> (foldr (lambda (v l) (cons (add1 v) l)) '() '(1 2 3 4))

'(2 3 4 5)

4.10.4 List Filtering🔗

procedure

(filter pred lst)  list?

  pred : procedure?
  lst : list?
返回一个包含 lstpred 产生真值的元素的 list。pred 过程按从第一个到最后一个的顺序应用于每个元素。

Example:
> (filter positive? '(1 -2 3 4 -5))

'(1 3 4)

procedure

(remove v lst [proc])  list?

  v : any/c
  lst : list?
  proc : procedure? = equal?
返回一个类似于 lst 的 list,使用比较过程 proc(必须接受两个参数),以 v 作为第一个参数,lst 中的元素作为第二个参数,省略 lst 中第一个与 v 相等的元素。如果 lst 中没有元素与 v 相等(根据 proc),则原样返回 lst

Examples:
> (remove 2 (list 1 2 3 2 4))

'(1 3 2 4)

> (remove '(2) (list '(1) '(2) '(3)))

'((1) (3))

> (remove "2" (list "1" "2" "3"))

'("1" "3")

> (remove #\c (list #\a #\b #\c))

'(#\a #\b)

> (remove "B" (list "a" "A" "b" "B") string-ci=?)

'("a" "A" "B")

> (remove 5 (list 1 2 3 2 4))

'(1 2 3 2 4)

Changed in version 8.2.0.2 of package base: 保证如果没有发生移除,输出与 lsteq? 的。

procedure

(remq v lst)  list?

  v : any/c
  lst : list?
返回 (remove v lst eq?)

Examples:
> (remq 2 (list 1 2 3 4 5))

'(1 3 4 5)

> (remq '(2) (list '(1) '(2) '(3)))

'((1) (2) (3))

> (remq "2" (list "1" "2" "3"))

'("1" "3")

> (remq #\c (list #\a #\b #\c))

'(#\a #\b)

procedure

(remv v lst)  list?

  v : any/c
  lst : list?
返回 (remove v lst eqv?)

Examples:
> (remv 2 (list 1 2 3 4 5))

'(1 3 4 5)

> (remv '(2) (list '(1) '(2) '(3)))

'((1) (2) (3))

> (remv "2" (list "1" "2" "3"))

'("1" "3")

> (remv #\c (list #\a #\b #\c))

'(#\a #\b)

procedure

(remw v lst)  list?

  v : any/c
  lst : list?
返回 (remove v lst equal-always?)

Examples:
> (remw 2 (list 1 2 3 4 5))

'(1 3 4 5)

> (remw '(2) (list '(1) '(2) '(3)))

'((1) (3))

> (remw "2" (list "1" "2" "3"))

'("1" "3")

> (remw #\c (list #\a #\b #\c))

'(#\a #\b)

> (define b1 (box 5))
> (define b2 (box 5))
> (remw b2 (list 0 b1 1 b2 2))

'(0 #&5 1 2)

Added in version 8.5.0.3 of package base.

procedure

(remove* v-lst lst [proc])  list?

  v-lst : list?
  lst : list?
  proc : procedure? = equal?
类似于 remove,但从 lst 中移除 v-lst 每个元素的每个实例。

Example:
> (remove* (list 1 2) (list 1 2 3 2 4 5 2))

'(3 4 5)

Changed in version 8.2.0.2 of package base: Guaranteed that the output is eq? to lst if no removal occurs.

procedure

(remq* v-lst lst)  list?

  v-lst : list?
  lst : list?
返回 (remove* v-lst lst eq?)

Example:
> (remq* (list 1 2) (list 1 2 3 2 4 5 2))

'(3 4 5)

procedure

(remv* v-lst lst)  list?

  v-lst : list?
  lst : list?
返回 (remove* v-lst lst eqv?)

Example:
> (remv* (list 1 2) (list 1 2 3 2 4 5 2))

'(3 4 5)

procedure

(remw* v-lst lst)  list?

  v-lst : list?
  lst : list?
返回 (remove* v-lst lst equal-always?)

Examples:
> (remw* (list 1 2) (list 1 2 3 2 4 5 2))

'(3 4 5)

> (define b1 (box 5))
> (define b2 (box 5))
> (remw* (list b2) (list 0 b1 1 b2 2 b2 3))

'(0 #&5 1 2 3)

Added in version 8.5.0.3 of package base.

procedure

(sort lst    
  less-than?    
  [#:key extract-key    
  #:cache-keys? cache-keys?])  list?
  lst : list?
  less-than? : (any/c any/c . -> . any/c)
  extract-key : (any/c . -> . any/c) = (lambda (x) x)
  cache-keys? : boolean? = #f
返回按 less-than? 过程排序的 list,该过程接受 lst 的两个元素,如果第一个小于第二个(即应该排在前面)则返回真值。

排序是稳定的;如果 lst 的两个元素“相等”(即 less-than? 以任一顺序给定该对时均不返回真值),则这些元素在输出 list 中保持其相对于 lst 的相对顺序。为保持此保证,请将 sort 与严格比较函数(例如 <string<?;而非 <=string<=?)一起使用。

由于 IEEE-754 数字系统规定 +nan.0 既不大于也不小于也不等于任何其他数字这一特殊事实,排序包含此值的 list 可能会产生令人惊讶的结果。

#:key 参数 extract-key 用于从每个 list 元素中提取用于比较的键值。也就是说,完整的比较过程本质上是

(lambda (x y)
  (less-than? (extract-key x) (extract-key y)))

默认情况下,每次比较时 extract-key 被应用于两个 list 元素,但如果 cache-keys? 为真,则 extract-key 函数对每个 list 项恰好使用一次。当 extract-key 是开销较大的操作时,为 cache-keys? 提供真值;例如,如果使用 file-or-directory-modify-seconds 为 list 中的每个文件提取时间戳,则 cache-keys? 应为 #t 以最小化文件系统调用,但如果 extract-keycar,则 cache-keys? 应为 #f。再举一个例子,提供 extract-key(lambda (x) (random)) 并将 #t 用于 cache-keys? 实际上会打乱 list。

Examples:
> (sort '(1 3 4 2) <)

'(1 2 3 4)

> (sort '("aardvark" "dingo" "cow" "bear") string<?)

'("aardvark" "bear" "cow" "dingo")

> (sort '(("aardvark") ("dingo") ("cow") ("bear"))
        #:key car string<?)

'(("aardvark") ("bear") ("cow") ("dingo"))

4.10.5 List Searching🔗

procedure

(member v lst [is-equal?])  (or/c #f list? any/c)

  v : any/c
  lst : (or/c list? any/c)
  is-equal? : (any/c any/c -> any/c) = equal?
定位 lst 中第一个与 v equal? 的元素。如果该元素存在,则返回从该元素开始的 lst 的尾部。否则,结果为 #f

lst 参数实际上不必是 list;lst 只需以 pair 链开始直到找到匹配元素。如果未找到匹配元素,则 lst 必须是 list(且不是循环 list)。如果找到元素且返回的 lst 尾部不是 list,则结果可以是非 list。

Examples:
> (member 2 (list 1 2 3 4))

'(2 3 4)

> (member 9 (list 1 2 3 4))

#f

> (member #'x (list #'x #'y) free-identifier=?)

'(#<syntax:eval:575:0 x> #<syntax:eval:575:0 y>)

> (member #'a (list #'x #'y) free-identifier=?)

#f

> (member 'b '(a b . etc))

'(b . etc)

procedure

(memw v lst)  (or/c #f list? any/c)

  v : any/c
  lst : (or/c list? any/c)
类似于 member,但使用 equal-always? 查找元素。

Examples:
> (memw 2 (list 1 2 3 4))

'(2 3 4)

> (memw 9 (list 1 2 3 4))

#f

> (define b1 (box 5))
> (define b2 (box 5))
> (memw b2 (list 0 b1 1 b2 2))

'(#&5 2)

Added in version 8.5.0.3 of package base.

procedure

(memv v lst)  (or/c #f list? any/c)

  v : any/c
  lst : (or/c list? any/c)
类似于 member,但使用 eqv? 查找元素。

Examples:
> (memv 2 (list 1 2 3 4))

'(2 3 4)

> (memv 9 (list 1 2 3 4))

#f

procedure

(memq v lst)  (or/c #f list? any/c)

  v : any/c
  lst : (or/c list? any/c)
类似于 member,但使用 eq? 查找元素。

Examples:
> (memq 2 (list 1 2 3 4))

'(2 3 4)

> (memq 9 (list 1 2 3 4))

#f

procedure

(memf proc lst)  (or/c #f list? any/c)

  proc : procedure?
  lst : (or/c list? any/c)
类似于 member,但使用谓词 proc 查找元素;当 proc 应用于该元素时返回真值则找到该元素。

Example:
> (memf (lambda (arg)
          (> arg 9))
        '(7 8 9 10 11))

'(10 11)

procedure

(findf proc lst)  any/c

  proc : procedure?
  lst : list?
类似于 memf,但返回元素或 #f,而不是 lst 的尾部或 #f

Example:
> (findf (lambda (arg)
           (> arg 9))
         '(7 8 9 10 11))

10

procedure

(assoc v lst [is-equal?])  (or/c pair? #f)

  v : any/c
  lst : (or/c (listof pair?) any/c)
  is-equal? : (any/c any/c -> any/c) = equal?
根据 is-equal? 定位 lst 中第一个 carv 相等的元素。如果该元素存在,则返回该 pair(即 lst 的一个元素)。否则,结果为 #f

lst 参数实际上不必是 pair 的 list;lst 只需以包含 pair 的 pair 链开始直到找到匹配元素。如果未找到匹配元素,则 lst 必须是 pair 的 list(且不是循环 list)。

Examples:
> (assoc 3 (list (list 1 2) (list 3 4) (list 5 6)))

'(3 4)

> (assoc 9 (list (list 1 2) (list 3 4) (list 5 6)))

#f

> (assoc 3.5
         (list (list 1 2) (list 3 4) (list 5 6))
         (lambda (a b) (< (abs (- a b)) 1)))

'(3 4)

procedure

(assw v lst)  (or/c pair? #f)

  v : any/c
  lst : (or/c (listof pair?) any/c)
类似于 assoc,但使用 equal-always? 查找元素。

Examples:
> (assw 3 (list (list 1 2) (list 3 4) (list 5 6)))

'(3 4)

> (define b1 (box 0))
> (define b2 (box 0))
> (assw b2 (list (cons b1 1) (cons b2 2)))

'(#&0 . 2)

Added in version 8.5.0.3 of package base.

procedure

(assv v lst)  (or/c pair? #f)

  v : any/c
  lst : (or/c (listof pair?) any/c)
类似于 assoc,但使用 eqv? 查找元素。

Example:
> (assv 3 (list (list 1 2) (list 3 4) (list 5 6)))

'(3 4)

procedure

(assq v lst)  (or/c pair? #f)

  v : any/c
  lst : (or/c (listof pair?) any/c)
类似于 assoc,但使用 eq? 查找元素。

Example:
> (assq 'c (list (list 'a 'b) (list 'c 'd) (list 'e 'f)))

'(c d)

procedure

(assf proc lst)  (or/c pair? #f)

  proc : procedure?
  lst : (or/c (listof pair?) any/c)
类似于 assoc,但使用谓词 proc 查找元素;当 proc 应用于 lst 元素的 car 时返回真值则找到该元素。

Example:
> (assf (lambda (arg)
          (> arg 2))
        (list (list 1 2) (list 3 4) (list 5 6)))

'(3 4)

4.10.6 Pair Accessor Shorthands🔗

procedure

(caar v)  any/c

  v : (cons/c pair? any/c)
Returns (car (car v)).

Example:
> (caar '((1 2) 3 4))

1

procedure

(cadr v)  any/c

  v : (cons/c any/c pair?)
Returns (car (cdr v)).

Example:
> (cadr '((1 2) 3 4))

3

procedure

(cdar v)  any/c

  v : (cons/c pair? any/c)
Returns (cdr (car v)).

Example:
> (cdar '((7 6 5 4 3 2 1) 8 9))

'(6 5 4 3 2 1)

procedure

(cddr v)  any/c

  v : (cons/c any/c pair?)
Returns (cdr (cdr v)).

Example:
> (cddr '(2 1))

'()

procedure

(caaar v)  any/c

  v : (cons/c (cons/c pair? any/c) any/c)
Returns (car (car (car v))).

Example:
> (caaar '(((6 5 4 3 2 1) 7) 8 9))

6

procedure

(caadr v)  any/c

  v : (cons/c any/c (cons/c pair? any/c))
Returns (car (car (cdr v))).

Example:
> (caadr '(9 (7 6 5 4 3 2 1) 8))

7

procedure

(cadar v)  any/c

  v : (cons/c (cons/c any/c pair?) any/c)
Returns (car (cdr (car v))).

Example:
> (cadar '((7 6 5 4 3 2 1) 8 9))

6

procedure

(caddr v)  any/c

  v : (cons/c any/c (cons/c any/c pair?))
Returns (car (cdr (cdr v))).

Example:
> (caddr '(3 2 1))

1

procedure

(cdaar v)  any/c

  v : (cons/c (cons/c pair? any/c) any/c)
Returns (cdr (car (car v))).

Example:
> (cdaar '(((6 5 4 3 2 1) 7) 8 9))

'(5 4 3 2 1)

procedure

(cdadr v)  any/c

  v : (cons/c any/c (cons/c pair? any/c))
Returns (cdr (car (cdr v))).

Example:
> (cdadr '(9 (7 6 5 4 3 2 1) 8))

'(6 5 4 3 2 1)

procedure

(cddar v)  any/c

  v : (cons/c (cons/c any/c pair?) any/c)
Returns (cdr (cdr (car v))).

Example:
> (cddar '((7 6 5 4 3 2 1) 8 9))

'(5 4 3 2 1)

procedure

(cdddr v)  any/c

  v : (cons/c any/c (cons/c any/c pair?))
Returns (cdr (cdr (cdr v))).

Example:
> (cdddr '(3 2 1))

'()

procedure

(caaaar v)  any/c

  v : (cons/c (cons/c (cons/c pair? any/c) any/c) any/c)
Returns (car (car (car (car v)))).

Example:
> (caaaar '((((5 4 3 2 1) 6) 7) 8 9))

5

procedure

(caaadr v)  any/c

  v : (cons/c any/c (cons/c (cons/c pair? any/c) any/c))
Returns (car (car (car (cdr v)))).

Example:
> (caaadr '(9 ((6 5 4 3 2 1) 7) 8))

6

procedure

(caadar v)  any/c

  v : (cons/c (cons/c any/c (cons/c pair? any/c)) any/c)
Returns (car (car (cdr (car v)))).

Example:
> (caadar '((7 (5 4 3 2 1) 6) 8 9))

5

procedure

(caaddr v)  any/c

  v : (cons/c any/c (cons/c any/c (cons/c pair? any/c)))
Returns (car (car (cdr (cdr v)))).

Example:
> (caaddr '(9 8 (6 5 4 3 2 1) 7))

6

procedure

(cadaar v)  any/c

  v : (cons/c (cons/c (cons/c any/c pair?) any/c) any/c)
Returns (car (cdr (car (car v)))).

Example:
> (cadaar '(((6 5 4 3 2 1) 7) 8 9))

5

procedure

(cadadr v)  any/c

  v : (cons/c any/c (cons/c (cons/c any/c pair?) any/c))
Returns (car (cdr (car (cdr v)))).

Example:
> (cadadr '(9 (7 6 5 4 3 2 1) 8))

6

procedure

(caddar v)  any/c

  v : (cons/c (cons/c any/c (cons/c any/c pair?)) any/c)
Returns (car (cdr (cdr (car v)))).

Example:
> (caddar '((7 6 5 4 3 2 1) 8 9))

5

procedure

(cadddr v)  any/c

  v : (cons/c any/c (cons/c any/c (cons/c any/c pair?)))
Returns (car (cdr (cdr (cdr v)))).

Example:
> (cadddr '(4 3 2 1))

1

procedure

(cdaaar v)  any/c

  v : (cons/c (cons/c (cons/c pair? any/c) any/c) any/c)
Returns (cdr (car (car (car v)))).

Example:
> (cdaaar '((((5 4 3 2 1) 6) 7) 8 9))

'(4 3 2 1)

procedure

(cdaadr v)  any/c

  v : (cons/c any/c (cons/c (cons/c pair? any/c) any/c))
Returns (cdr (car (car (cdr v)))).

Example:
> (cdaadr '(9 ((6 5 4 3 2 1) 7) 8))

'(5 4 3 2 1)

procedure

(cdadar v)  any/c

  v : (cons/c (cons/c any/c (cons/c pair? any/c)) any/c)
Returns (cdr (car (cdr (car v)))).

Example:
> (cdadar '((7 (5 4 3 2 1) 6) 8 9))

'(4 3 2 1)

procedure

(cdaddr v)  any/c

  v : (cons/c any/c (cons/c any/c (cons/c pair? any/c)))
Returns (cdr (car (cdr (cdr v)))).

Example:
> (cdaddr '(9 8 (6 5 4 3 2 1) 7))

'(5 4 3 2 1)

procedure

(cddaar v)  any/c

  v : (cons/c (cons/c (cons/c any/c pair?) any/c) any/c)
Returns (cdr (cdr (car (car v)))).

Example:
> (cddaar '(((6 5 4 3 2 1) 7) 8 9))

'(4 3 2 1)

procedure

(cddadr v)  any/c

  v : (cons/c any/c (cons/c (cons/c any/c pair?) any/c))
Returns (cdr (cdr (car (cdr v)))).

Example:
> (cddadr '(9 (7 6 5 4 3 2 1) 8))

'(5 4 3 2 1)

procedure

(cdddar v)  any/c

  v : (cons/c (cons/c any/c (cons/c any/c pair?)) any/c)
Returns (cdr (cdr (cdr (car v)))).

Example:
> (cdddar '((7 6 5 4 3 2 1) 8 9))

'(4 3 2 1)

procedure

(cddddr v)  any/c

  v : (cons/c any/c (cons/c any/c (cons/c any/c pair?)))
Returns (cdr (cdr (cdr (cdr v)))).

Example:
> (cddddr '(4 3 2 1))

'()

4.10.7 Additional List Functions and Synonyms🔗

 (require racket/list) package: base
The bindings documented in this section are provided by the racket/list and racket libraries, but not racket/base.

value

empty : null?

空 list。

Examples:
> empty

'()

> (eq? empty null)

#t

procedure

(cons? v)  boolean?

  v : any/c
(pair? v) 相同。

Example:
> (cons? '(1 2))

#t

procedure

(empty? v)  boolean?

  v : any/c
(null? v) 相同。

Examples:
> (empty? '(1 2))

#f

> (empty? '())

#t

procedure

(first lst)  any/c

  lst : list?
(car lst) 相同,但仅适用于非空 list。

Example:
> (first '(1 2 3 4 5 6 7 8 9 10))

1

procedure

(rest lst)  list?

  lst : list?
(cdr lst) 相同,但仅适用于非空 list。

Example:
> (rest '(1 2 3 4 5 6 7 8 9 10))

'(2 3 4 5 6 7 8 9 10)

procedure

(second lst)  any

  lst : list?
返回 list 的第二个元素。

Example:
> (second '(1 2 3 4 5 6 7 8 9 10))

2

procedure

(third lst)  any

  lst : list?
返回 list 的第三个元素。

Example:
> (third '(1 2 3 4 5 6 7 8 9 10))

3

procedure

(fourth lst)  any

  lst : list?
返回 list 的第四个元素。

Example:
> (fourth '(1 2 3 4 5 6 7 8 9 10))

4

procedure

(fifth lst)  any

  lst : list?
返回 list 的第五个元素。

Example:
> (fifth '(1 2 3 4 5 6 7 8 9 10))

5

procedure

(sixth lst)  any

  lst : list?
返回 list 的第六个元素。

Example:
> (sixth '(1 2 3 4 5 6 7 8 9 10))

6

procedure

(seventh lst)  any

  lst : list?
返回 list 的第七个元素。

Example:
> (seventh '(1 2 3 4 5 6 7 8 9 10))

7

procedure

(eighth lst)  any

  lst : list?
返回 list 的第八个元素。

Example:
> (eighth '(1 2 3 4 5 6 7 8 9 10))

8

procedure

(ninth lst)  any

  lst : list?
返回 list 的第九个元素。

Example:
> (ninth '(1 2 3 4 5 6 7 8 9 10))

9

procedure

(tenth lst)  any

  lst : list?
返回 list 的第十个元素。

Example:
> (tenth '(1 2 3 4 5 6 7 8 9 10))

10

procedure

(last lst)  any

  lst : list?
返回 list 的最后一个元素。

此函数的时间与 lst 的长度成正比。

Example:
> (last '(1 2 3 4 5 6 7 8 9 10))

10

procedure

(last-pair p)  pair?

  p : pair?
返回(可能是非正规的)list 的最后一个 pair。

此函数的时间与 p 的“长度”成正比。

Example:
> (last-pair '(1 2 3 4))

'(4)

procedure

(make-list k v)  list?

  k : exact-nonnegative-integer?
  v : any/c
返回一个新构造的长度为 k 的 list,所有位置都包含 v

Example:
> (make-list 7 'foo)

'(foo foo foo foo foo foo foo)

procedure

(list-update lst pos updater)  list?

  lst : list?
  pos : (and/c (>=/c 0) (</c (length lst)))
  updater : (-> any/c any/c)
返回一个与 lst 相同的 list,但在指定索引处不同。指定索引处的元素为 (updater (list-ref lst pos))

此函数的时间与 pos 成正比。

Example:
> (list-update '(zero one two) 1 symbol->string)

'(zero "one" two)

Added in version 6.3 of package base.

procedure

(list-set lst pos value)  list?

  lst : list?
  pos : (and/c (>=/c 0) (</c (length lst)))
  value : any/c
返回一个与 lst 相同的 list,但在指定索引处不同。指定索引处的元素为 value

此函数的时间与 pos 成正比。

Example:
> (list-set '(zero one two) 2 "two")

'(zero one "two")

Added in version 6.3 of package base.

procedure

(index-of lst v [is-equal?])  (or/c exact-nonnegative-integer? #f)

  lst : list?
  v : any/c
  is-equal? : (any/c any/c . -> . any/c) = equal?
类似于 member,但返回找到的第一个元素的索引,而不是 list 的尾部。

Example:
> (index-of '(1 2 3 4) 3)

2

Added in version 6.7.0.3 of package base.

procedure

(index-where lst proc)  (or/c exact-nonnegative-integer? #f)

  lst : list?
  proc : (any/c . -> . any/c)
类似于 index-of,但具有 memf 的谓词搜索行为。

Example:
> (index-where '(1 2 3 4) even?)

1

Added in version 6.7.0.3 of package base.

procedure

(indexes-of lst v [is-equal?])

  (listof exact-nonnegative-integer?)
  lst : list?
  v : any/c
  is-equal? : (any/c any/c . -> . any/c) = equal?
类似于 index-of,但返回元素在 list 中出现的所有索引的 list,而不仅仅是第一个。

Example:
> (indexes-of '(1 2 1 2 1) 2)

'(1 3)

Added in version 6.7.0.3 of package base.

procedure

(indexes-where lst proc)  (listof exact-nonnegative-integer?)

  lst : list?
  proc : (any/c . -> . any/c)
类似于 indexes-of,但具有 index-where 的谓词搜索行为。

Example:
> (indexes-where '(1 2 3 4) even?)

'(1 3)

Added in version 6.7.0.3 of package base.

procedure

(take lst pos)  list?

  lst : any/c
  pos : exact-nonnegative-integer?
返回一个新 list,其元素为 lst 的前 pos 个元素。如果 lst 的元素少于 pos 个,则 exn:fail:contract exception is raised。

lst 参数实际上不必是 list;lst 只需以至少 pos 个 pair 的链开始。

此函数的时间与 pos 成正比。

Examples:
> (take '(1 2 3 4 5) 2)

'(1 2)

> (take 'non-list 0)

'()

procedure

(drop lst pos)  any/c

  lst : any/c
  pos : exact-nonnegative-integer?
list-tail 相同。

procedure

(split-at lst pos)  
list? any/c
  lst : any/c
  pos : exact-nonnegative-integer?
返回与以下相同的结果

(values (take lst pos) (drop lst pos))

除了它可能更快,但它仍需要与 pos 成正比的时间。

procedure

(takef lst pred)  list?

  lst : any/c
  pred : procedure?
返回一个新 list,其元素从 lst 中依次取出,只要它们满足 pred。返回的 list 包含直到(但不包括)lstpred 返回 #f 的第一个元素。

lst 参数实际上不必是 list;lst 中的 pair 链将被遍历直到遇到非 pair。

Examples:
> (takef '(2 4 5 8) even?)

'(2 4)

> (takef '(2 4 6 8) odd?)

'()

> (takef '(2 4 . 6) even?)

'(2 4)

procedure

(dropf lst pred)  any/c

  lst : any/c
  pred : procedure?
lst 的前端丢弃元素,只要它们满足 pred

Examples:
> (dropf '(2 4 5 8) even?)

'(5 8)

> (dropf '(2 4 6 8) odd?)

'(2 4 6 8)

procedure

(splitf-at lst pred)  
list? any/c
  lst : any/c
  pred : procedure?
返回与以下相同的结果

(values (takef lst pred) (dropf lst pred))

除了它可能更快。

procedure

(take-right lst pos)  any/c

  lst : any/c
  pos : exact-nonnegative-integer?
返回 listpos 长度尾部。如果 lst 的元素少于 pos 个,则 exn:fail:contract exception is raised。

lst 参数实际上不必是 list;lst 只需以至少 pos 个 pair 的链结束。

此函数的时间与 lst 的长度成正比。

Examples:
> (take-right '(1 2 3 4 5) 2)

'(4 5)

> (take-right 'non-list 0)

'non-list

procedure

(drop-right lst pos)  list?

  lst : any/c
  pos : exact-nonnegative-integer?
返回一个新 list,其元素为 lst 的前缀,丢弃其 pos 长度尾部。如果 lst 的元素少于 pos 个,则 exn:fail:contract exception is raised。

lst 参数实际上不必是 list;lst 只需以至少 pos 个 pair 的链结束。

此函数的时间与 lst 的长度成正比。

Examples:
> (drop-right '(1 2 3 4 5) 2)

'(1 2 3)

> (drop-right 'non-list 0)

'()

procedure

(split-at-right lst pos)  
list? any/c
  lst : any/c
  pos : exact-nonnegative-integer?
返回与以下相同的结果

(values (drop-right lst pos) (take-right lst pos))

除了它可能更快,但它仍需要与 lst 长度成正比的时间。

Examples:
> (split-at-right '(1 2 3 4 5 6) 3)

'(1 2 3)

'(4 5 6)

> (split-at-right '(1 2 3 4 5 6) 4)

'(1 2)

'(3 4 5 6)

procedure

(takef-right lst pred)  any/c

  lst : any/c
  pred : procedure?

procedure

(dropf-right lst pred)  list?

  lst : any/c
  pred : procedure?

procedure

(splitf-at-right lst pred)  
list? any/c
  lst : any/c
  pred : procedure?
类似于 takefdropfsplitf-at,但结合了 take-rightdrop-rightsplit-at-right 的从右操作功能。

procedure

(list-prefix? l r [same?])  boolean?

  l : list?
  r : list?
  same? : (any/c any/c . -> . any/c) = equal?
如果 lr 的前缀则返回 #t

Example:
> (list-prefix? '(1 2) '(1 2 3 4 5))

#t

Added in version 6.3 of package base.

procedure

(take-common-prefix l r [same?])  list?

  l : list?
  r : list?
  same? : (any/c any/c . -> . any/c) = equal?
返回 lr 的最长公共前缀。

Example:
> (take-common-prefix '(a b c d) '(a b x y z))

'(a b)

Added in version 6.3 of package base.

procedure

(drop-common-prefix l r [same?])  
list? list?
  l : list?
  r : list?
  same? : (any/c any/c . -> . any/c) = equal?
返回移除公共前缀后 lr 的尾部。

Example:
> (drop-common-prefix '(a b c d) '(a b x y z))

'(c d)

'(x y z)

Added in version 6.3 of package base.

procedure

(split-common-prefix l r [same?])  
list? list? list?
  l : list?
  r : list?
  same? : (any/c any/c . -> . any/c) = equal?
返回最长公共前缀以及移除公共前缀后 lr 的尾部。

Example:
> (split-common-prefix '(a b c d) '(a b x y z))

'(a b)

'(c d)

'(x y z)

Added in version 6.3 of package base.

procedure

(add-between lst    
  v    
  [#:before-first before-first    
  #:before-last before-last    
  #:after-last after-last    
  #:splice? splice?])  list?
  lst : list?
  v : any/c
  before-first : list? = '()
  before-last : any/c = v
  after-last : list? = '()
  splice? : any/c = #f
返回一个与 lst 具有相同元素的 list,但在 lst 的每对元素之间插入 v;最后一对元素之间将使用 before-last 而非 v(但 before-last 默认为 v)。

如果 splice? 为真,则 vbefore-last 应为 list,且 list 元素被拼接到结果中。此外,当 splice? 为真时,before-firstafter-last 分别在第一个元素之前和最后一个元素之后插入。

Examples:
> (add-between '(x y z) 'and)

'(x and y and z)

> (add-between '(x) 'and)

'(x)

> (add-between '("a" "b" "c" "d") "," #:before-last "and")

'("a" "," "b" "," "c" "and" "d")

> (add-between '(x y z) '(-) #:before-last '(- -)
               #:before-first '(begin) #:after-last '(end LF)
               #:splice? #t)

'(begin x - y - - z end LF)

procedure

(append* lst ... lsts)  list?

  lst : list?
  lsts : (listof list?)
(append* lst ... lsts)  any/c
  lst : list?
  lsts : list?
类似于 append,但最后一个参数用作 append 的参数 list,因此 (append* lst ... lsts)(apply append lst ... lsts) 相同。换句话说,appendappend* 之间的关系类似于 listlist* 之间的关系。

Examples:
> (append* '(a) '(b) '((c) (d)))

'(a b c d)

> (cdr (append* (map (lambda (x) (list ", " x))
                     '("Alpha" "Beta" "Gamma"))))

'("Alpha" ", " "Beta" ", " "Gamma")

procedure

(flatten v)  list?

  v : any/c
将任意 S-表达式的 pair 结构展平为 list。更准确地说,v 被视为二叉树,其中 pair 是内部节点,结果 list 包含树中所有非 null 的叶子,其顺序与中序遍历相同。

Examples:
> (flatten '((a) b (c (d) . e) ()))

'(a b c d e)

> (flatten 'a)

'(a)

procedure

(check-duplicates lst    
  [same?    
  #:key extract-key    
  #:default failure-result])  any
  lst : list?
  same? : (any/c any/c . -> . any/c) = equal?
  extract-key : (-> any/c any/c) = (lambda (x) x)
  failure-result : failure-result/c = (lambda () #f)
返回 lst 中第一个重复项。更准确地说,它返回第一个 x,使得存在之前的 y 满足 (same? (extract-key x) (extract-key y))

如果未找到重复项,则由 failure-result 决定结果:

  • 如果 failure-result 是过程,则通过尾调用不带参数地调用它来产生结果。

  • 否则,返回 failure-result 作为结果。

same? 参数应是一个等价谓词,如 equal?eqv?,或一个字典。过程 equal?eqv?eq? 会自动使用字典以提高速度。

Examples:
> (check-duplicates '(1 2 3 4))

#f

> (check-duplicates '(1 2 3 2 1))

2

> (check-duplicates '((a 1) (b 2) (a 3)) #:key car)

'(a 3)

> (check-duplicates '(1 2 3 4 5 6)
                    (lambda (x y) (equal? (modulo x 3) (modulo y 3))))

4

> (check-duplicates '(1 2 3 4) #:default "no duplicates")

"no duplicates"

Added in version 6.3 of package base.
Changed in version 6.11.0.2: 添加了 #:default 可选参数。

procedure

(remove-duplicates lst    
  [same?    
  #:key extract-key])  list?
  lst : list?
  same? : (any/c any/c . -> . any/c) = equal?
  extract-key : (any/c . -> . any/c) = (lambda (x) x)
返回一个包含 lst 中所有项但没有重复项的 list,其中 same? 决定 list 中两个元素是否等价。结果 list 的顺序与 lst 相同,对于多次出现的项,保留第一个。

#:key 参数 extract-key 用于从每个 list 元素中提取键值,因此如果 (same? (extract-key x) (extract-key y)) 为真,则两个项被视为相等。

Examples:
> (remove-duplicates '(a b b a))

'(a b)

> (remove-duplicates '(1 2 1.0 0))

'(1 2 1.0 0)

> (remove-duplicates '(1 2 1.0 0) =)

'(1 2 0)

procedure

(filter-map proc lst ...+)  list?

  proc : procedure?
  lst : list?
类似于 (map proc lst ...),但不同之处在于,如果 proc 返回 #false,则该元素从结果 list 中省略。换句话说,filter-map 等价于 (filter (lambda (x) x) (map proc lst ...)),但更高效,因为 filter-map 避免了构建中间 list。

Example:
> (filter-map (lambda (x) (and (negative? x) (abs x))) '(1 2 -3 -4 8))

'(3 4)

procedure

(count proc lst ...+)  exact-nonnegative-integer?

  proc : procedure?
  lst : list?
返回 (length (filter-map proc lst ...)),但不构建中间 list。

Example:
> (count positive? '(1 -1 2 3 -2 5))

4

procedure

(partition pred lst)  
list? list?
  pred : procedure?
  lst : list?
类似于 filter,但返回两个值:pred 返回真值的项,以及 pred 返回 #f 的项。

结果与以下相同

(values (filter pred lst) (filter (negate pred) lst))

pred 仅对 lst 中的每项应用一次。

Example:
> (partition even? '(1 2 3 4 5 6))

'(2 4 6)

'(1 3 5)

procedure

(range end)  list?

  end : real?
(range start end [step])  list?
  start : real?
  end : real?
  step : real? = 1
类似于 in-range,但返回 list。

结果 list 包含从 start 开始的数字,其后续元素通过将 step 加到前一个元素来计算,直到达到 end(不包含)。如果未提供起始点,则使用 0。如果未提供 step 参数,则使用 1

in-range 类似,当 range 应用直接出现在 for 子句中时,可以提供更好的性能。

Examples:
> (range 10)

'(0 1 2 3 4 5 6 7 8 9)

> (range 10 20)

'(10 11 12 13 14 15 16 17 18 19)

> (range 20 40 2)

'(20 22 24 26 28 30 32 34 36 38)

> (range 20 10 -1)

'(20 19 18 17 16 15 14 13 12 11)

> (range 10 15 1.5)

'(10 11.5 13.0 14.5)

Changed in version 6.7.0.4 of package base: 调整为与 for 配合使用,方式与 in-range 相同。

procedure

(inclusive-range start end [step])  list?

  start : real?
  end : real?
  step : real? = 1
类似于 in-inclusive-range,但返回 list。

结果 list 包含从 start 开始的数字,其后续元素通过将 step 加到前一个元素来计算,直到达到 end(包含)。如果未提供 step 参数,则使用 1

in-inclusive-range 类似,当 inclusive-range 应用直接出现在 for 子句中时,可以提供更好的性能。

Examples:
> (inclusive-range 10 20)

'(10 11 12 13 14 15 16 17 18 19 20)

> (inclusive-range 20 40 2)

'(20 22 24 26 28 30 32 34 36 38 40)

> (inclusive-range 20 10 -1)

'(20 19 18 17 16 15 14 13 12 11 10)

> (inclusive-range 10 15 1.5)

'(10 11.5 13.0 14.5)

Added in version 8.0.0.13 of package base.

procedure

(append-map proc lst ...+)  list?

  proc : procedure?
  lst : list?
返回 (append* (map proc lst ...))

Example:
> (append-map vector->list '(#(1) #(2 3) #(4)))

'(1 2 3 4)

procedure

(filter-not pred lst)  list?

  pred : (any/c . -> . any/c)
  lst : list?
类似于 filter,但 pred 谓词的含义被反转:结果是 pred 返回 #f 的所有项的 list。

Example:
> (filter-not even? '(1 2 3 4 5 6))

'(1 3 5)

procedure

(shuffle lst)  list?

  lst : list?
返回一个包含 lst 中所有元素的 list,随机打乱顺序。

Examples:
> (shuffle '(1 2 3 4 5 6))

'(1 4 2 3 5 6)

> (shuffle '(1 2 3 4 5 6))

'(2 6 4 1 5 3)

> (shuffle '(1 2 3 4 5 6))

'(3 5 2 1 4 6)

procedure

(combinations lst)  list?

  lst : list?
(combinations lst size)  list?
  lst : list?
  size : exact-nonnegative-integer?

Wikipedia combinations

返回输入 list 中元素的所有组合的 list(即 lst幂集)。如果给定 size,则将结果限制为 size 个元素的组合。

Examples:
> (combinations '(1 2 3))

'(() (1) (2) (1 2) (3) (1 3) (2 3) (1 2 3))

> (combinations '(1 2 3) 2)

'((1 2) (1 3) (2 3))

procedure

(in-combinations lst)  sequence?

  lst : list?
(in-combinations lst size)  sequence?
  lst : list?
  size : exact-nonnegative-integer?
返回输入 list 中元素的所有组合的序列,如果给定 size,则返回所有长度为 size 的组合。逐个构建组合,而不是一次性全部构建。

Examples:
> (time (begin (combinations (range 15)) (void)))

cpu time: 3 real time: 3 gc time: 1

> (time (begin (in-combinations (range 15)) (void)))

cpu time: 0 real time: 0 gc time: 0

procedure

(permutations lst)  list?

  lst : list?
返回输入 list 的所有排列的 list。请注意,此函数在不检查元素的情况下工作,因此它忽略重复元素(这将导致重复排列)。如果输入 list 包含超过 256 个元素,则引发错误。

Examples:
> (permutations '(1 2 3))

'((1 2 3) (2 1 3) (1 3 2) (3 1 2) (2 3 1) (3 2 1))

> (permutations '(x x))

'((x x) (x x))

procedure

(in-permutations lst)  sequence?

  lst : list?
返回输入 list 的所有排列的序列。它等价于 (in-list (permutations l)),但由于在每次迭代中逐个构建排列,因此速度要快得多。如果输入 list 包含超过 256 个元素,则引发错误。

procedure

(argmin proc lst)  any/c

  proc : (-> any/c real?)
  lst : (and/c pair? list?)
返回 list lst 中使 proc 结果最小化的第一个元素。对空 list 发出错误信号。另见 min

Examples:
> (argmin car '((3 pears) (1 banana) (2 apples)))

'(1 banana)

> (argmin car '((1 banana) (1 orange)))

'(1 banana)

procedure

(argmax proc lst)  any/c

  proc : (-> any/c real?)
  lst : (and/c pair? list?)
返回 list lst 中使 proc 结果最大化的第一个元素。对空 list 发出错误信号。另见 max

Examples:
> (argmax car '((3 pears) (1 banana) (2 apples)))

'(3 pears)

> (argmax car '((3 pears) (3 oranges)))

'(3 pears)

procedure

(group-by key lst [same?])  (listof list?)

  key : (-> any/c any/c)
  lst : list?
  same? : (any/c any/c . -> . any/c) = equal?
将给定 list 分组为等价类,等价性由 same? 决定。在每个等价类中,group-by 保持原始 list 的顺序。等价类本身按在输入中首次出现的顺序排列。

Example:
> (group-by (lambda (x) (modulo x 3)) '(1 2 1 2 54 2 5 43 7 2 643 1 2 0))

'((1 1 43 7 643 1) (2 2 2 5 2 2) (54 0))

Added in version 6.3 of package base.

procedure

(cartesian-product lst ...)  (listof list?)

  lst : list?
计算给定 list 的 n 元笛卡尔积。

Examples:
> (cartesian-product '(1 2 3) '(a b c))

'((1 a) (1 b) (1 c) (2 a) (2 b) (2 c) (3 a) (3 b) (3 c))

> (cartesian-product '(4 5 6) '(d e f) '(#t #f))

'((4 d #t)

  (4 d #f)

  (4 e #t)

  (4 e #f)

  (4 f #t)

  (4 f #f)

  (5 d #t)

  (5 d #f)

  (5 e #t)

  (5 e #f)

  (5 f #t)

  (5 f #f)

  (6 d #t)

  (6 d #f)

  (6 e #t)

  (6 e #f)

  (6 f #t)

  (6 f #f))

Added in version 6.3 of package base.

procedure

(remf pred lst)  list?

  pred : procedure?
  lst : list?
返回一个类似于 lst 的 list,省略 lstpred 产生真值的第一个元素。

Example:
> (remf negative? '(1 -2 3 4 -5))

'(1 3 4 -5)

Added in version 6.3 of package base.

procedure

(remf* pred lst)  list?

  pred : procedure?
  lst : list?
类似于 remf,但移除 pred 产生真值的所有元素。

Example:
> (remf* negative? '(1 -2 3 4 -5))

'(1 3 4)

Added in version 6.3 of package base.

4.10.8 Immutable Cyclic Data🔗

procedure

(make-reader-graph v)  any/c

  v : any/c
返回一个类似于 v 的值,其中由 make-placeholder 创建的 placeholder(占位符)被它们包含的值替换,由 make-hash-placeholder 创建的 hash placeholder(哈希占位符)被不可变哈希表替换。v 的任何部分都不会被修改;相反,v 的部分会根据需要复制以构造结果图,其中对于任何给定值最多创建一个副本。

由于复制的值可以是不可变的,并且副本也是不可变的,因此 make-reader-graph 可以创建仅涉及不可变 pair、向量、盒子和哈希表的循环。

只有以下类型的值会被复制和遍历以检测 placeholder:

由于这些限制,make-reader-graph 创建的循环值与 read 完全相同。

Example:
> (let* ([ph (make-placeholder #f)]
         [x (cons 1 ph)])
    (placeholder-set! ph x)
    (make-reader-graph x))

#0='(1 . #0#)

procedure

(placeholder? v)  boolean?

  v : any/c
如果 v 是由 make-placeholder 创建的 placeholder,则返回 #t,否则返回 #f

procedure

(make-placeholder v)  placeholder?

  v : any/c
返回一个用于 placeholder-set!make-reader-graphplaceholderv 参数为 placeholder 提供初始值。

procedure

(placeholder-set! ph datum)  void?

  ph : placeholder?
  datum : any/c
ph 的值更改为 v

procedure

(placeholder-get ph)  any/c

  ph : placeholder?
返回 ph 的值。

procedure

(hash-placeholder? v)  boolean?

  v : any/c
如果 v 是由 make-hash-placeholder 创建的 hash placeholder,则返回 #t,否则返回 #f

procedure

(make-hash-placeholder assocs)  hash-placeholder?

  assocs : (listof pair?)
类似于 make-immutable-hash,但产生用于 make-reader-graphhash placeholder

procedure

(make-hasheq-placeholder assocs)  hash-placeholder?

  assocs : (listof pair?)
类似于 make-immutable-hasheq,但产生用于 make-reader-graphhash placeholder

procedure

(make-hasheqv-placeholder assocs)  hash-placeholder?

  assocs : (listof pair?)
类似于 make-immutable-hasheqv,但产生用于 make-reader-graphhash placeholder

procedure

(make-hashalw-placeholder assocs)  hash-placeholder?

  assocs : (listof pair?)
类似于 make-immutable-hashalw,但产生用于 make-reader-graphhash placeholder

Added in version 8.5.0.3 of package base.