4.3 Lists
(require rebellion/collection/list) | package: rebellion |
The rebellion/collection/list library is a small wrapper module designed to provide a more modern and conventional interface to standard Racket lists. Additionally, it provides a few utilities for integrating lists with some of Rebellion’s standard abstractions, such as reducers. No attempt is made to wrap every possible list-related function or rename every export of racket/list, as many of these operations are better expressed in terms of generic sequences, for loops, reducers, and transducers rather than lists specifically.
For the purposes of this library, "list" means "proper list". Improper lists (such as those constructed by list*) are a poor fit for most list-related tasks and should be avoided in favor of other data structures.
procedure
(empty-list? v) → boolean?
v : any/c
procedure
(nonempty-list? v) → boolean?
v : any/c
procedure
(list-first lst) → list?
lst : nonempty-list?
> (list-first (list 1 2 3)) 1
> (list-first empty-list) list-first: contract violation
expected: nonempty-list?
given: '()
in: the 1st argument of
(-> nonempty-list? any/c)
contract from:
<pkgs>/rebellion/private/list.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/rebellion/private/list.rkt:11.3
procedure
lst : nonempty-list?
> (list-rest (list 1 2 3)) '(2 3)
> (list-rest empty-list) list-rest: contract violation
expected: nonempty-list?
given: '()
in: the 1st argument of
(-> nonempty-list? (listof any/c))
contract from:
<pkgs>/rebellion/private/list.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/rebellion/private/list.rkt:12.3
procedure
(list-ref-safe lst position) → option?
lst : list? position : natural?
> (list-ref-safe (list 'a 'b 'c) 0) (present 'a)
> (list-ref-safe (list 'a 'b 'c) 2) (present 'c)
> (list-ref-safe (list 'a 'b 'c) 5) #<absent>
> (list-size (list 'a 'b 'c)) 3
> (list-size empty-list) 0
procedure
(list-insert lst v) → nonempty-list?
lst : list? v : any/c
> (list-insert (list 2 3 4) 1) '(1 2 3 4)
procedure
(list-contains? lst v) → boolean?
lst : list? v : any/c
> (list-contains? (list 1 2 3 4 5) 2) #t
> (list-contains? (list 1 2 3 4 5) 1000) #f
procedure
(list-append lst ...) → list?
lst : list?
> (list-append (list 1 2 3) empty-list (list 4 5) (list 6 7 8 9)) '(1 2 3 4 5 6 7 8 9)
> (list-append (list 1 2 3)) '(1 2 3)
> (list-append) '()
procedure
(list-reverse lst) → list?
lst : list?
> (list-reverse (list 1 2 3 4 5)) '(5 4 3 2 1)
> (list-reverse empty-list) '()
value
empty-list : empty-list? = (list)
value
> (reduce into-reversed-list 1 2 3 4 5) '(5 4 3 2 1)
value
> (reduce append-into-list (list 1 2 3) (list 'a 'b) empty-list (list 4 5 6 7 8)) '(1 2 3 a b 4 5 6 7 8)