5 VList
(require pfds/vlist) | package: pfds |
A VList is a data structure very similar to a normal 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.
syntax
(List A)
> (list 1 2 3 4 5 6) - : (List Positive-Byte)
#<List>
In the above example, the vlist obtained will have 1 as its first element.
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 (list 1 2 3 4 5 6)) - : Integer [more precisely: Positive-Byte]
1
> (first empty) first: 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 (list 1 2 3 4 5 6) 0) - : Integer [more precisely: Positive-Byte]
1
> (list-ref (list 1 2 3 4 5 6) 3) - : Integer [more precisely: Positive-Byte]
4
> (list-ref (list 1 2 3 4 5 6) 10) list-ref: given index out of bounds
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).
procedure
(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.
> (foldl + 0 (list 1 2 3 4 5 6)) - : Integer [more precisely: Nonnegative-Integer]
21
> (foldl * 1 (list 1 2 3 4 5 6) (list 1 2 3 4 5 6)) - : Integer [more precisely: Positive-Integer]
518400
procedure
(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.
> (foldr + 0 (list 1 2 3 4 5 6)) - : Integer [more precisely: Nonnegative-Integer]
21
> (foldr * 1 (list 1 2 3 4 5 6) (list 1 2 3 4 5 6)) - : Integer [more precisely: Positive-Integer]
518400
procedure
(andmap func lst1 lst2 ...) → Boolean
func : (A B ... B -> Boolean) lst1 : (List A) lst2 : (List B)
> (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
procedure
(ormap func lst1 lst2 ...) → Boolean
func : (A B ... B -> Boolean) lst1 : (List A) lst2 : (List B)
> (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
procedure
(build-list size func) → (List A)
size : Natural func : (Natural -> A)
> (->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)