6 VList
(require "vlist.ss") |
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 Binary Random Access List.
(list a ...) → (List A) |
a : 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.
empty : (List Nothing) |
(empty? vlst) → Boolean |
vlst : (List A) |
Examples: |
> (empty? (list 1 2 3 4 5 6)) |
- : Boolean |
#f |
> (empty? empty) |
- : Boolean |
#t |
(cons a vlst) → (List A) |
a : A |
vlst : (List A) |
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).
(first vlst) → A |
vlst : (List A) |
Examples: |
> (first (list 1 2 3 4 5 6)) |
- : Positive-Fixnum |
1 |
> (first empty) |
first: given vlist is empty |
(last vlst) → A |
vlst : (List A) |
Examples: |
> (last (list 1 2 3 4 5 6)) |
- : Positive-Fixnum |
6 |
> (last empty) |
last: given vlist is empty |
(rest vlst) → (List A) |
vlst : (List A) |
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).
(list-ref vlst index) → A |
vlst : (List A) |
index : Integer |
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 |
(length vlst) → Integer |
vlst : (List A) |
Examples: |
> (length (list 1 2 3 4 5 6)) |
- : Integer |
6 |
> (length empty) |
- : Integer |
0 |
(vlist->list vlst) → (Listof A) |
vlst : (List A) |
Examples: |
> (vlist->list (list 1 2 3 4 5 6)) |
- : (Listof Positive-Fixnum) |
'(1 2 3 4 5 6) |
> (vlist->list empty) |
- : (Listof Nothing) |
'() |
(reverse vlst) → (List A) |
vlst : (List A) |
Example: |
> (vlist->list (reverse (list 1 2 3 4 5 6))) |
- : (Listof Positive-Fixnum) |
'(6 5 4 3 2 1) |
In the above example, (reverse (list 1 2 3 4 5 6)), returns the reversed vlist (list 6 5 4 3 2 1).
(map func vlst1 vlst2 ...) → (List A) |
func : (A B ... B -> C) |
vlst1 : (List A) |
vlst2 : (List B) |
Examples: |
> (vlist->list (map add1 (list 1 2 3 4 5 6))) |
- : (Listof Exact-Positive-Integer) |
'(2 3 4 5 6 7) |
> (vlist->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 |
(filter func vlst) → (List A) |
func : (A -> Boolean) |
vlst : (List A) |
Examples: |
> (define vlst (list 1 2 3 4 5 6)) |
> (vlist->list (filter (λ:([x : Integer]) (> x 5)) vlst)) |
- : (Listof Positive-Fixnum) |
'(6) |
> (vlist->list (filter (λ:([x : Integer]) (< x 5)) vlst)) |
- : (Listof Positive-Fixnum) |
'(1 2 3 4) |
> (vlist->list (filter (λ:([x : Integer]) (<= x 4)) vlst)) |
- : (Listof Positive-Fixnum) |
'(1 2 3 4) |