5 Catenable List
(require "catenablelist.ss") |
Catenable Lists are nothing but lists with efficient catenation. They use a data-structucal bootstrapping technique called Structucal Abstraction. The data structure internally use Real Time Queues to realize an amortized running time of O(1) for the operations first, rest, cons and cons-to-end.
(list a ...) → (CatenableList A) |
a : A |
Example: |
> (list 1 2 3 4 5 6) |
- : (U EmptyList (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 EmptyList (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 EmptyList (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 EmptyList (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) |
'() |
(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) |
'(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) |
(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 |
(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) |