6 VList
(require (planet krhari/pfds:1:5/vlist)) |
A VList is a data structure very similar to noraml Scheme List but the corresponding operations are significantly faster for most of the List operations. Indexing and length operations have a running time of O(1) and O(lg N) respectively compared to O(N) in lists. The data structure has been described in the paper Fast Functional Lists, Hash-Lists, vlists and Variable Length Arrays by Phil Bagwell. VLists implementation internally uses Skew Binary Random Access List.
(List A) |
Example: |
> (list 1 2 3 4 5 6) |
- : (List Positive-Fixnum) |
#<List> |
In the above example, the vlist obtained will have 1 as its first element.
Examples: |
> (empty? (list 1 2 3 4 5 6)) |
- : Boolean |
#f |
> (empty? empty) |
- : Boolean |
#t |
In the above example, cons adds the element 10 to (list 1 2 3 4 5 6) and returns (list 10 1 2 3 4 5 6).
Examples: |
> (first (list 1 2 3 4 5 6)) |
- : Positive-Fixnum |
1 |
> (first empty) |
first: given vlist is empty |
Examples: |
> (last (list 1 2 3 4 5 6)) |
- : Positive-Fixnum |
6 |
> (last empty) |
last: given vlist is empty |
Examples: |
> (rest (list 1 2 3 4 5 6)) |
- : (List Positive-Fixnum) |
#<List> |
> (rest empty) |
rest: given vlist is empty |
In the above example, (rest (list 1 2 3 4 5 6)), removes the head and returns (list 2 3 4 5 6).
Examples: |
> (list-ref (list 1 2 3 4 5 6) 0) |
- : Positive-Fixnum |
1 |
> (list-ref (list 1 2 3 4 5 6) 3) |
- : Positive-Fixnum |
4 |
> (list-ref (list 1 2 3 4 5 6) 10) |
list-ref: given index out of bounds |
Examples: |
> (length (list 1 2 3 4 5 6)) |
- : Integer |
6 |
> (length empty) |
- : Integer |
0 |
Examples: |
> (->list (list 1 2 3 4 5 6)) |
- : (Listof Positive-Fixnum) |
'(1 2 3 4 5 6) |
> (->list empty) |
- : (Listof Nothing) |
'() |
Example: |
> (->list (reverse (list 1 2 3 4 5 6))) |
- : (Listof Positive-Fixnum) |
'(6 5 4 3 2 1) |
Examples: |
> (->list (map add1 (list 1 2 3 4 5 6))) |
- : (Listof Exact-Positive-Integer) |
'(2 3 4 5 6 7) |
> (->list (map * (list 1 2 3 4 5 6) (list 1 2 3 4 5 6))) |
- : (Listof Exact-Positive-Integer) |
'(1 4 9 16 25 36) |
In the above example, (map add1 (list 1 2 3 4 5 6)) adds 1 to each element of the given vlist and returns (list 2 3 4 5 6 7). (map * (list 1 2 3 4 5 6) (list 1 2 3 4 5 6)) multiplies corresponding elements in the two vlists and returns the vlist (list 1 4 9 16 25 36).
(foldl func init vlst1 vlst2 ...) → C |
func : (C A B ... B -> C) |
init : C |
vlst1 : (List A) |
vlst2 : (List B) |
foldl currently does not produce correct results when the given function is non-commutative.
Examples: |
> (foldl + 0 (list 1 2 3 4 5 6)) |
- : Exact-Nonnegative-Integer |
21 |
> (foldl * 1 (list 1 2 3 4 5 6) (list 1 2 3 4 5 6)) |
- : Exact-Positive-Integer |
518400 |
(foldr func init vlst1 vlst2 ...) → C |
func : (C A B ... B -> C) |
init : C |
vlst1 : (List A) |
vlst2 : (List B) |
foldr currently does not produce correct results when the given function is non-commutative.
Examples: |
> (foldr + 0 (list 1 2 3 4 5 6)) |
- : Exact-Nonnegative-Integer |
21 |
> (foldr * 1 (list 1 2 3 4 5 6) (list 1 2 3 4 5 6)) |
- : Exact-Positive-Integer |
518400 |
Examples: |
> (andmap even? (list 1 2 3 4 5 6)) |
- : Boolean |
#f |
> (andmap odd? (list 1 2 3 4 5 6)) |
- : Boolean |
#f |
> (andmap positive? (list 1 2 3 4 5 6)) |
- : Boolean |
#t |
> (andmap negative? (list -1 -2)) |
- : Boolean |
#t |
Examples: |
> (ormap even? (list 1 2 3 4 5 6)) |
- : Boolean |
#t |
> (ormap odd? (list 1 2 3 4 5 6)) |
- : Boolean |
#t |
> (ormap positive? (list -1 -2 3 4 -5 6)) |
- : Boolean |
#t |
> (ormap negative? (list 1 -2)) |
- : Boolean |
#t |
(build-list size func) → (List A) |
size : Natural |
func : (Natural -> A) |
Examples: |
> (->list (build-list 5 (λ:([x : Integer]) (add1 x)))) |
- : (Listof Integer) |
'(1 2 3 4 5) |
> (->list (build-list 5 (λ:([x : Integer]) (* x x)))) |
- : (Listof Integer) |
'(0 1 4 9 16) |
Examples: |
> (->list (make-list 5 10)) |
- : (Listof Positive-Fixnum) |
'(10 10 10 10 10) |
> (->list (make-list 5 'sym)) |
- : (Listof 'sym) |
'(sym sym sym sym sym) |
Examples: |
> (define vlst (list 1 2 3 4 5 6)) |
> (->list (filter (λ:([x : Integer]) (> x 5)) vlst)) |
- : (Listof Positive-Fixnum) |
'(6) |
> (->list (filter (λ:([x : Integer]) (< x 5)) vlst)) |
- : (Listof Positive-Fixnum) |
'(1 2 3 4) |
> (->list (filter (λ:([x : Integer]) (<= x 4)) vlst)) |
- : (Listof Positive-Fixnum) |
'(1 2 3 4) |
Example: |
> (second (list 1 2 3 4 5 6 7 8 9 10)) |
1- : Positive-Fixnum |
2 |
Example: |
> (third (list 1 2 3 4 5 6 7 8 9 10)) |
2- : Positive-Fixnum |
3 |
Example: |
> (fourth (list 1 2 3 4 5 6 7 8 9 10)) |
- : Positive-Fixnum |
4 |
Example: |
> (fifth (list 1 2 3 4 5 6 7 8 9 10)) |
- : Positive-Fixnum |
5 |
Example: |
> (sixth (list 1 2 3 4 5 6 7 8 9 10)) |
- : Positive-Fixnum |
6 |
Example: |
> (seventh (list 1 2 3 4 5 6 7 8 9 10)) |
- : Positive-Fixnum |
7 |
Example: |
> (eighth (list 1 2 3 4 5 6 7 8 9 10)) |
- : Positive-Fixnum |
8 |
Example: |
> (ninth (list 1 2 3 4 5 6 7 8 9 10)) |
- : Positive-Fixnum |
9 |
Example: |
> (tenth (list 1 2 3 4 5 6 7 8 9 10 11)) |
- : Positive-Fixnum |
10 |