1 Bindings
(require data/ralist) | package: ralist |
1.1 Checked and Unchecked contracts
This library provides bindings for list operations in two forms: the first assumes all contracts are met, the second checks.
The observable differences are in their failure modes—
The checked bindings are designed to be complete in their checking of contract properties, regardless of computational expense. The unchecked bindings are designed to be fast and to give reasonable error messages to any violation that can easily be detected. Given inputs that satisfy the contract, both versions produced equivalent results.
The main module provides bindings for list operations with unchecked contracts. Where appropriate, examples are given demonstrating the differences in failure modes for the checked and unchecked bindings. See the benchmark on Contracted vs. Uncontracted bindings for a performance comparison.
1.2 Pairs and Lists
A pair combines exactly two values. The first value is accessed with the car procedure, and the second value is accessed with the cdr procedure. Pairs are not mutable.
A list is recursively defined: it is either the constant empty, or it is a pair whose second value is a list.
A list can be used as a single-valued sequence (see Sequences). The elements of the list serve as elements of the sequence. See also in-list.
1.3 Sequences
Random-access lists implement the sequence interface, so (list? x) implies (sequence? x), and elements of a list may be extracted with any of the for syntactic forms.
> (in-list (list 1 2 3)) (1 2 3)
> (for/fold ([sum 0] [rev-roots empty]) ([i (in-list (list 1 2 3 4))]) (values (+ sum i) (cons (sqrt i) rev-roots)))
10
(2 1.7320508075688772 1.4142135623730951 1)
1.4 Iterations and Comprehensions
syntax
(for/list ([id sequence-expr] ...) body ...+)
> (for/list ([i '(1 2 3)] [j "abc"] #:when (odd? i) [k #(#t #f)]) (list i j k)) ((1 #\a #t) (1 #\a #f) (3 #\c #t) (3 #\c #f))
> (for/list () 'any) (any)
> (for/list ([i '()]) (error "doesn't get here")) '()
1.5 Match patterns
Coming soon.
1.6 Values
> empty '()
> (first+rest (list 1 2 3))
1
(2 3)
> (first+rest empty) first+rest: contract violation
expected: ra:cons?
given: '()
in: an and/c case of
the 1st argument of
(-> (and/c ra:cons? ra:list?) any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:47.2
> (first+rest (cons 1 2)) first+rest: contract violation
expected: ra:list?
given: (1 . 2)
in: an and/c case of
the 1st argument of
(-> (and/c ra:cons? ra:list?) any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:47.2
> (first+rest empty) ra:first+rest: expected cons, given: ()
> (first+rest (cons 1 2))
1
2
> (first empty) first: contract violation
expected: ra:cons?
given: '()
in: an and/c case of
the 1st argument of
(-> (and/c ra:cons? ra:list?) any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:48.2
> (first (cons 'x 'y)) first: contract violation
expected: ra:list?
given: (x . y)
in: an and/c case of
the 1st argument of
(-> (and/c ra:cons? ra:list?) any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:48.2
> (rest empty) rest: contract violation
expected: ra:cons?
given: '()
in: an and/c case of
the 1st argument of
(-> (and/c ra:cons? ra:list?) any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:49.2
> (rest (cons 'x 'y)) rest: contract violation
expected: ra:list?
given: (x . y)
in: an and/c case of
the 1st argument of
(-> (and/c ra:cons? ra:list?) any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:49.2
> (car+cdr empty) car+cdr: contract violation
expected: ra:cons?
given: '()
in: the 1st argument of
(-> ra:cons? any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:50.2
> (car empty) car: contract violation
expected: ra:cons?
given: '()
in: the 1st argument of
(-> ra:cons? any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:51.2
> (cdr empty) cdr: contract violation
expected: ra:cons?
given: '()
in: the 1st argument of
(-> ra:cons? any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:52.2
procedure
xs : (count>/c i) i : natural-number/c
> (list-ref (list 'x 'y 'z) 3) list-ref: contract violation
expected: (count>/c 3)
given: (x y z)
in: the domain of
(->d ((x ...) (y ...)) () any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:81.2
> (list-ref (list* 'x 'y 'z) 2) list-ref: contract violation
expected: (count>/c 2)
given: (x y . z)
in: the domain of
(->d ((x ...) (y ...)) () any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:81.2
> (list-ref (list 'x 'y 'z) 3) kons-size: contract violation
expected: kons?
given: '()
> (list-ref (list* 'x 'y 'z) 2) kons-size: contract violation
expected: kons?
given: 'z
procedure
xs : (count>/c i) i : natural-number/c x : any/c
> (list-set (list 'x 'y 'z) 3 'd) list-set: contract violation
expected: (count>/c 3)
given: (x y z)
in: the domain of
(->d ((x ...) (y ...) (z ...)) () any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:86.2
> (list-set (list* 'x 'y 'z) 2 'c) list-set: contract violation
expected: (count>/c 2)
given: (x y . z)
in: the domain of
(->d ((x ...) (y ...) (z ...)) () any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:86.2
> (list-set (list 'x 'y 'z) 3 'd) kons-size: contract violation
expected: kons?
given: '()
> (list-set (list* 'x 'y 'z) 2 'c) kons-size: contract violation
expected: kons?
given: 'z
procedure
(list-update xs i f) → cons?
xs : (count>/c i) i : natural-number/c f : (procedure-arity-includes/c 1)
procedure
(list-ref/set xs i v) →
any/c cons? xs : (count>/c i) i : natural-number/c v : any/c
procedure
(list-ref/update xs i f) →
any/c cons? xs : (count>/c i) i : natural-number/c f : (procedure-arity-includes/c 1)
> (second (list 'a 'b)) 'b
> (third (list 'a 'b 'c)) 'c
> (fourth (list 'a 'b 'c 'd)) 'd
> (fifth (list 'a 'b 'c 'd 'e)) 'e
> (sixth (list 'a 'b 'c 'd 'e 'f)) 'f
> (seventh (list 'a 'b 'c 'd 'e 'f 'g)) 'g
> (eighth (list 'a 'b 'c 'd 'e 'f 'g 'h)) 'h
> (ninth (list 'a 'b 'c 'd 'e 'f 'g 'h 'i)) 'i
> (tenth (list 'a 'b 'c 'd 'e 'f 'g 'h 'i 'j)) 'j
> (second (list* 'a 'b 'c)) second: contract violation
expected: ra:list?
given: (a b . c)
in: an and/c case of
the 1st argument of
(-> (and/c ra:list? (count>/c 1)) any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:63.2
> (second (list 'a)) second: contract violation
expected: (count>/c 1)
given: (a)
in: an and/c case of
the 1st argument of
(-> (and/c ra:list? (count>/c 1)) any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:63.2
> (second (list* 'a 'b 'c)) 'b
> (second (list 'a)) kons-size: contract violation
expected: kons?
given: '()
> (last empty) last: contract violation
expected: ra:cons?
given: '()
in: an and/c case of
the 1st argument of
(-> (and/c ra:cons? ra:list?) any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:72.2
> (last (list* 1 2 3)) last: contract violation
expected: ra:list?
given: (1 2 . 3)
in: an and/c case of
the 1st argument of
(-> (and/c ra:cons? ra:list?) any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:72.2
procedure
f :
(or/c (is-true/c (zero? (count xs))) (procedure-arity-includes/c (add1 (mz:length ...)))) xs : (and/c list? (count=/c (count xs)))
> (map add1 (list 1 2 3) (list 4 5 6)) map: contract violation
expected: (or/c (is-true/c (zero? (count xs)))
(procedure-arity-includes/c 2))
given: #<procedure:add1>
in: the domain of
(->d ((x ...) (y ...)) () #:rest z ... any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:120.2
> (map + (list 1 2 3) (list 4)) map: contract violation
expected: (count=/c 3)
given: (4)
in: an and/c case of
an element of
the domain of
(->d ((x ...) (y ...)) () #:rest z ... any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:120.2
> (map add1 (list 1 2 3) (list 4 5 6)) add1: arity mismatch;
the expected number of arguments does not match the given
number
expected: 1
given: 2
arguments...:
1
4
> (map + (list 1 2 3) (list 4)) +: contract violation
expected: number?
given: '#s(node 1 2 3)
argument position: 1st
other arguments...:
4
procedure
f :
(or/c (is-true/c (zero? (count xs))) (procedure-arity-includes/c (+ 2 (mz:length ...)))) b : any/c xs : list?
procedure
f :
(or/c (is-true/c (zero? (count xs))) (procedure-arity-includes/c (+ 2 (mz:length ...)))) a : any/c xs : list?
procedure
f :
(or/c (is-true/c (zero? (count xs))) (procedure-arity-includes/c (add1 (mz:length ...)))) xs : (and/c list? (count=/c (count xs)))
procedure
f :
(or/c (is-true/c (zero? (count xs))) (procedure-arity-includes/c (add1 (mz:length ...)))) xs : (and/c list? (count=/c (count xs)))
procedure
n : natural-number/c x : any/c
procedure
(build-list n f) → list?
n : natural-number/c
f :
(or/c (is-true/c (zero? n)) (procedure-arity-includes/c 1))
> (build-list 0 'not-function) '()
> (build-list 0 (lambda (i) i)) '()
> (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)
> (build-list 5 'not-function) build-list: contract violation
expected: (or/c (is-true/c (zero? n))
(procedure-arity-includes/c 1))
given: 'not-function
in: the domain of
(->d ((x ...) (y ...)) () any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:113.2
> (build-list 5 'not-function) application: not a procedure;
expected a procedure that can be applied to arguments
given: 'not-function
arguments...:
2
procedure
(length xs) → natural-number/c
xs : list?
> (length (list* 1 2 3)) length: contract violation
expected: ra:list?
given: (1 2 . 3)
in: the 1st argument of
(-> ra:list? any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:59.2
procedure
(count xs) → natural-number/c
xs : any/c
procedure
xs : list? i : natural-number/c
> (append 3 5) append: contract violation
expected: ra:list?
given: 3
in: an element of
the rest argument of
(->* () #:rest (listof ra:list?) any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:60.2
> (append 1 (list 2 3)) append: contract violation
expected: ra:list?
given: 1
in: an element of
the rest argument of
(->* () #:rest (listof ra:list?) any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:60.2
> (append 3 5) ra:first+rest: expected cons, given: 3
> (append 1 (list 2 3)) ra:first+rest: expected cons, given: 1
> (reverse (list* 1 2 3)) reverse: contract violation
expected: ra:list?
given: (1 2 . 3)
in: the 1st argument of
(-> ra:list? any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:61.2
procedure
n : natural-number/c
> (range (list 1 2 3)) range: contract violation
expected: natural-number/c
given: (1 2 3)
in: the 1st argument of
(-> natural-number/c any)
contract from:
<pkgs>/ralist/data/ralist/contract.rkt
blaming: top-level
(assuming the contract is correct)
at: <pkgs>/ralist/data/ralist/contract.rkt:62.2