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。
通过 read 或 make-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
(build-list n proc) → list?
n : exact-nonnegative-integer? proc : (exact-nonnegative-integer? . -> . any)
> (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?
procedure
lst : pair? pos : exact-nonnegative-integer?
lst 参数实际上不必是 list;lst 只需以至少 (add1 pos) 个 pair 的链开始。
此函数的时间与 pos 成正比。
> (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
lst : any/c pos : exact-nonnegative-integer?
lst 参数实际上不必是 list;lst 只需以至少 pos 个 pair 的链开始。
此函数的时间与 pos 成正比。
> (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
最后一个参数不必是 list,在这种情况下结果是“非正规 list”(improper list)。
此函数的时间与除最后一个参数外所有参数的长度总和成正比。
> (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)
此函数的时间与 lst 的长度成正比。
4.10.3 List Iteration
procedure
proc : procedure? lst : list?
> (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
proc : procedure? lst : list?
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。
> (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
proc : procedure? lst : list?
继续上面 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。
procedure
proc : procedure? lst : list?
procedure
proc : procedure? init : any/c lst : list?
如果使用 n 个 list 调用 foldl,则 proc 必须接受 n+1 个参数。额外的参数是到目前为止的组合返回值。proc 最初以每个 list 的第一个元素调用,最后一个参数为 init。在后续调用中,最后一个参数是 proc 前一次调用的返回值。输入 lst 从左到右遍历,整个 foldl 应用的结果是 proc 最后一次应用的结果。如果 lst 为空,结果为 init。
与 foldr 不同,foldl 在常数空间内处理 lst(加上每次调用 proc 的空间)。
> (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
proc : procedure? init : any/c lst : list?
4.10.4 List Filtering
procedure
pred : procedure? lst : list?
> (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: 保证如果没有发生移除,输出与 lst 是 eq? 的。
> (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)
> (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)
> (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.
Changed in version 8.2.0.2 of package base: Guaranteed that the output is eq? to lst if no removal occurs.
> (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
排序是稳定的;如果 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-key 是 car,则 cache-keys? 应为 #f。再举一个例子,提供 extract-key 为 (lambda (x) (random)) 并将 #t 用于 cache-keys? 实际上会打乱 list。
4.10.5 List Searching
lst 参数实际上不必是 list;lst 只需以 pair 链开始直到找到匹配元素。如果未找到匹配元素,则 lst 必须是 list(且不是循环 list)。如果找到元素且返回的 lst 尾部不是 list,则结果可以是非 list。
> (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)
> (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
proc : procedure? lst : list?
lst 参数实际上不必是 pair 的 list;lst 只需以包含 pair 的 pair 链开始直到找到匹配元素。如果未找到匹配元素,则 lst 必须是 pair 的 list(且不是循环 list)。
> (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)
> (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.
4.10.6 Pair Accessor Shorthands
> (caar '((1 2) 3 4)) 1
> (cadr '((1 2) 3 4)) 3
> (cdar '((7 6 5 4 3 2 1) 8 9)) '(6 5 4 3 2 1)
> (cddr '(2 1)) '()
> (caaar '(((6 5 4 3 2 1) 7) 8 9)) 6
> (caadr '(9 (7 6 5 4 3 2 1) 8)) 7
> (cadar '((7 6 5 4 3 2 1) 8 9)) 6
> (caddr '(3 2 1)) 1
> (cdaar '(((6 5 4 3 2 1) 7) 8 9)) '(5 4 3 2 1)
> (cdadr '(9 (7 6 5 4 3 2 1) 8)) '(6 5 4 3 2 1)
> (cddar '((7 6 5 4 3 2 1) 8 9)) '(5 4 3 2 1)
> (cdddr '(3 2 1)) '()
> (caaaar '((((5 4 3 2 1) 6) 7) 8 9)) 5
> (caaadr '(9 ((6 5 4 3 2 1) 7) 8)) 6
> (caadar '((7 (5 4 3 2 1) 6) 8 9)) 5
> (caaddr '(9 8 (6 5 4 3 2 1) 7)) 6
> (cadaar '(((6 5 4 3 2 1) 7) 8 9)) 5
> (cadadr '(9 (7 6 5 4 3 2 1) 8)) 6
> (caddar '((7 6 5 4 3 2 1) 8 9)) 5
> (cadddr '(4 3 2 1)) 1
> (cdaaar '((((5 4 3 2 1) 6) 7) 8 9)) '(4 3 2 1)
> (cdaadr '(9 ((6 5 4 3 2 1) 7) 8)) '(5 4 3 2 1)
> (cdadar '((7 (5 4 3 2 1) 6) 8 9)) '(4 3 2 1)
> (cdaddr '(9 8 (6 5 4 3 2 1) 7)) '(5 4 3 2 1)
> (cddaar '(((6 5 4 3 2 1) 7) 8 9)) '(4 3 2 1)
> (cddadr '(9 (7 6 5 4 3 2 1) 8)) '(5 4 3 2 1)
> (cdddar '((7 6 5 4 3 2 1) 8 9)) '(4 3 2 1)
> (cddddr '(4 3 2 1)) '()
4.10.7 Additional List Functions and Synonyms
| (require racket/list) | package: base |
> (cons? '(1 2)) #t
> (first '(1 2 3 4 5 6 7 8 9 10)) 1
> (rest '(1 2 3 4 5 6 7 8 9 10)) '(2 3 4 5 6 7 8 9 10)
> (second '(1 2 3 4 5 6 7 8 9 10)) 2
> (third '(1 2 3 4 5 6 7 8 9 10)) 3
> (fourth '(1 2 3 4 5 6 7 8 9 10)) 4
> (fifth '(1 2 3 4 5 6 7 8 9 10)) 5
> (sixth '(1 2 3 4 5 6 7 8 9 10)) 6
> (seventh '(1 2 3 4 5 6 7 8 9 10)) 7
> (eighth '(1 2 3 4 5 6 7 8 9 10)) 8
> (ninth '(1 2 3 4 5 6 7 8 9 10)) 9
> (tenth '(1 2 3 4 5 6 7 8 9 10)) 10
此函数的时间与 lst 的长度成正比。
> (last '(1 2 3 4 5 6 7 8 9 10)) 10
此函数的时间与 p 的“长度”成正比。
> (last-pair '(1 2 3 4)) '(4)
procedure
k : exact-nonnegative-integer? v : any/c
> (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)
此函数的时间与 pos 成正比。
> (list-update '(zero one two) 1 symbol->string) '(zero "one" two)
Added in version 6.3 of package base.
此函数的时间与 pos 成正比。
> (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?
> (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-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?
> (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-where '(1 2 3 4) even?) '(1 3)
Added in version 6.7.0.3 of package base.
procedure
lst : any/c pos : exact-nonnegative-integer?
lst 参数实际上不必是 list;lst 只需以至少 pos 个 pair 的链开始。
此函数的时间与 pos 成正比。
procedure
lst : any/c pos : exact-nonnegative-integer?
procedure
(split-at lst pos) →
list? any/c lst : any/c pos : exact-nonnegative-integer?
除了它可能更快,但它仍需要与 pos 成正比的时间。
procedure
lst : any/c pred : procedure?
lst 参数实际上不必是 list;lst 中的 pair 链将被遍历直到遇到非 pair。
procedure
lst : any/c pred : procedure?
procedure
(splitf-at lst pred) →
list? any/c lst : any/c pred : procedure?
除了它可能更快。
procedure
(take-right lst pos) → any/c
lst : any/c pos : exact-nonnegative-integer?
lst 参数实际上不必是 list;lst 只需以至少 pos 个 pair 的链结束。
此函数的时间与 lst 的长度成正比。
> (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?
lst 参数实际上不必是 list;lst 只需以至少 pos 个 pair 的链结束。
此函数的时间与 lst 的长度成正比。
> (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 长度成正比的时间。
> (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?
procedure
(list-prefix? l r [same?]) → boolean?
l : list? r : list? same? : (any/c any/c . -> . any/c) = equal?
> (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?
> (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?
> (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?
> (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
如果 splice? 为真,则 v 和 before-last 应为 list,且 list 元素被拼接到结果中。此外,当 splice? 为真时,before-first 和 after-last 分别在第一个元素之前和最后一个元素之后插入。
> (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)
> (append* '(a) '(b) '((c) (d))) '(a b c d)
> (cdr (append* (map (lambda (x) (list ", " x)) '("Alpha" "Beta" "Gamma")))) '("Alpha" ", " "Beta" ", " "Gamma")
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)
如果未找到重复项,则由 failure-result 决定结果:
如果 failure-result 是过程,则通过尾调用不带参数地调用它来产生结果。
否则,返回 failure-result 作为结果。
same? 参数应是一个等价谓词,如 equal? 或 eqv?,或一个字典。过程 equal?、eqv? 和 eq? 会自动使用字典以提高速度。
> (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)
#:key 参数 extract-key 用于从每个 list 元素中提取键值,因此如果 (same? (extract-key x) (extract-key y)) 为真,则两个项被视为相等。
> (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?
> (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?
procedure
(partition pred lst) →
list? list? pred : procedure? lst : list?
结果与以下相同
但 pred 仅对 lst 中的每项应用一次。
结果 list 包含从 start 开始的数字,其后续元素通过将 step 加到前一个元素来计算,直到达到 end(不包含)。如果未提供起始点,则使用 0。如果未提供 step 参数,则使用 1。
与 in-range 类似,当 range 应用直接出现在 for 子句中时,可以提供更好的性能。
> (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
结果 list 包含从 start 开始的数字,其后续元素通过将 step 加到前一个元素来计算,直到达到 end(包含)。如果未提供 step 参数,则使用 1。
与 in-inclusive-range 类似,当 inclusive-range 应用直接出现在 for 子句中时,可以提供更好的性能。
> (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 vector->list '(#(1) #(2 3) #(4))) '(1 2 3 4)
> (filter-not even? '(1 2 3 4 5 6)) '(1 3 5)
> (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
> (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?
> (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?
> (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?
> (argmin car '((3 pears) (1 banana) (2 apples))) '(1 banana)
> (argmin car '((1 banana) (1 orange))) '(1 banana)
> (argmax car '((3 pears) (1 banana) (2 apples))) '(3 pears)
> (argmax car '((3 pears) (3 oranges))) '(3 pears)
Added in version 6.3 of package base.
procedure
(cartesian-product lst ...) → (listof list?)
lst : list?
> (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
pred : procedure? lst : list?
Added in version 6.3 of package base.
procedure
pred : procedure? lst : list?
Added in version 6.3 of package base.
4.10.8 Immutable Cyclic Data
procedure
(make-reader-graph v) → any/c
v : any/c
由于复制的值可以是不可变的,并且副本也是不可变的,因此 make-reader-graph 可以创建仅涉及不可变 pair、向量、盒子和哈希表的循环。
只有以下类型的值会被复制和遍历以检测 placeholder:
pair
向量,包括可变和不可变的
盒子,包括可变和不可变的
哈希表,包括可变和不可变的
prefab 结构类型的实例
由 make-placeholder 和 make-hash-placeholder 创建的 placeholder
由于这些限制,make-reader-graph 创建的循环值与 read 完全相同。
> (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
procedure
(make-placeholder v) → placeholder?
v : any/c
procedure
(placeholder-set! ph datum) → void?
ph : placeholder? datum : any/c
procedure
(placeholder-get ph) → any/c
ph : placeholder?
procedure
(hash-placeholder? v) → boolean?
v : any/c
procedure
(make-hash-placeholder assocs) → hash-placeholder?
assocs : (listof pair?)
procedure
(make-hasheq-placeholder assocs) → hash-placeholder?
assocs : (listof pair?)
procedure
(make-hasheqv-placeholder assocs) → hash-placeholder?
assocs : (listof pair?)
procedure
(make-hashalw-placeholder assocs) → hash-placeholder?
assocs : (listof pair?)
Added in version 8.5.0.3 of package base.