5 Catenable List
(require (planet krhari/pfds:1:5/catenablelist)) |
Catenable Lists are nothing but lists with efficient catenation. They use a data-structucal bootstrapping technique called Structucal Abstraction. The data structure internally uses Bootstraped Queue to realize an amortized running time of O(1) for the operations first, rest, cons and cons-to-end.
(CatenableList A) |
(list a ...) → (CatenableList A) |
a : A |
Example: |
> (list 1 2 3 4 5 6) |
- : (U Null (List Positive-Fixnum)) |
#<List> |
In the above example, (list 1 2 3 4 5 6) gives a Catenable List which is similar to lists but comes with efficient append operation.
empty |
(empty? catlist) → Boolean |
catlist : (CatenableList A) |
(cons a catlist) → (CatenableList A) |
a : A |
catlist : (CatenableList A) |
In the above example, (cons 10 (list 1 2 3 4 5 6)) returns (list 10 1 2 3 4 5 6).
(cons-to-end a catlist) → (CatenableList A) |
a : A |
catlist : (CatenableList A) |
Example: |
> (cons-to-end 10 (list 1 2 3 4 5 6)) |
- : (U Null (List Positive-Fixnum)) |
#<List> |
In the above example, (cons-to-end 10 (list 1 2 3 4 5 6)) returns (list 1 2 3 4 5 6 10).
(first catlist) → A |
catlist : (CatenableList A) |
Examples: |
> (first (list 1 2 3 4 5 6)) |
- : Positive-Fixnum |
1 |
> (first empty) |
first: given list is empty |
(rest catlist) → (CatenableList A) |
catlist : (CatenableList A) |
Examples: |
> (rest (list 1 2 3 4 5 6)) |
- : (U Null (List Positive-Fixnum)) |
#<List> |
> (rest empty) |
rest: given list is empty |
In the above example, (rest (list 1 2 3 4 5 6)) returns the rest of the given catenable list, (list 2 3 4 5 6).
(append cal1 cal2) → (CatenableList A) |
cal1 : (CatenableList A) |
cal2 : (CatenableList A) |
Examples: |
> (define cal1 (list 1 2 3 4 5 6)) |
> (define cal2 (list 7 8 9 10)) |
> (append cal1 cal2) |
- : (U Null (List Positive-Fixnum)) |
#<List> |
In the above example, (append cal1 cal2) returns (list 1 2 3 4 5 6 7 8 9 10).
(->list cal) → (Listof A) |
cal : (CatenableList A) |
Examples: |
> (->list (list 1 2 3 4 5 6)) |
- : (Listof Positive-Fixnum) |
'(1 2 3 4 5 6) |
> (->list empty) |
- : (Listof Nothing) |
'() |
(reverse cal) → (List A) |
cal : (List A) |
Example: |
> (->list (reverse (list 1 2 3 4 5 6))) |
- : (Listof Positive-Fixnum) |
'(6 5 4 3 2 1) |
(map func clst1 clst2 ...) → (CatenableList A) |
func : (A B ... B -> C) |
clst1 : (CatenableList A) |
clst2 : (CatenableList B) |
Examples: |
> (->list (map add1 (list 1 2 3 4 5 6))) |
- : (Listof Exact-Positive-Integer) |
'(7 6 5 4 3 2) |
> (->list (map * (list 1 2 3 4 5 6) (list 1 2 3 4 5 6))) |
- : (Listof Exact-Positive-Integer) |
'(36 25 16 9 4 1) |
(foldl func init clst1 clst2 ...) → C |
func : (C A B ... B -> C) |
init : C |
clst1 : (CatenableList A) |
clst2 : (CatenableList 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 clst1 clst2 ...) → C |
func : (C A B ... B -> C) |
init : C |
clst1 : (CatenableList A) |
clst2 : (CatenableList 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 |
(andmap func lst1 lst2 ...) → Boolean |
func : (A B ... B -> Boolean) |
lst1 : (CatenableList A) |
lst2 : (CatenableList B) |
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 |
(ormap func lst1 lst2 ...) → Boolean |
func : (A B ... B -> Boolean) |
lst1 : (CatenableList A) |
lst2 : (CatenableList B) |
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) → (CatenableList 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) |
(make-list size func) → (CatenableList A) |
size : Natural |
func : A |
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) |
(filter func que) → (CatenableList A) |
func : (A -> Boolean) |
que : (CatenableList A) |
Examples: |
> (define que (list 1 2 3 4 5 6)) |
> (->list (filter (λ: ([x : Integer]) (> x 5)) que)) |
- : (Listof Positive-Fixnum) |
'(6) |
> (->list (filter (λ: ([x : Integer]) (< x 5)) que)) |
- : (Listof Positive-Fixnum) |
'(4 3 2 1) |
> (->list (filter (λ: ([x : Integer]) (<= x 5)) que)) |
- : (Listof Positive-Fixnum) |
'(5 4 3 2 1) |
(remove func que) → (CatenableList A) |
func : (A -> Boolean) |
que : (CatenableList A) |
Examples: | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(5 4 3 2 1) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(6 5) | ||
| ||
- : (Listof Positive-Fixnum) | ||
'(6) |