Lazy lists

Lists in Scheme and Lisp are eager. Since the procedure calling regime in these languages is "Call by value", a list argument of a procedure call is completely constructed before the procedure is called. This is fine for small lists, but it excludes practically the chaining of procedure calls with large list arguments. On the other hand such a chaining is a tremendously powerful modularization technique, as demonstrated by purely functional languages like Haskell.

The traditional tools for implementation of lazy evaluation consist of the two Scheme primitives delay and force (cf. the classic "Structure and Interpretation of Computer Porgrams" by Abelson and Sussman, usually abbreveated "SICP"). But there is a better method as shown by Moritz Heidkamp in his lazy-Lst Module, which in turn is meant to replace the stream datatype in SRFI-41. Moritz' approach is inspired by the Lisp dialect Clojure, which also motivated the beautiful macros in his clojurian module. The fundamental idea is to store the structure of a lazy list in a record, but to realize this list only as much as needed. This way a large (even infinite) list can be created instantaneously without realizing it and it will be realized only if and as much as used.

This module is based on Heidkamp's implementation with one essential addition: The length of the list is stored in the record and can thus be referenced without realizing the whole list. After all, some operations like reverse are only meaningful for finite lists, so one must know beforehand if a list is finite to avoid infinite loops.

But knowing the length of a list at the moment of its creation, lazy lists can replace ordinary lists as a datatype. And ordinary list operations can be replaced by lazy list operations. This is the reason for the other difference of this module with Moritz' lazy-Lst, a cosmetic difference: Lazy list operations are named with the same name as ordinary ones, only capitalized at the beginning. So Cons, Car, Cdr ... are the replacements of cons, car, cdr etc. Some operators have a different argument order, thow, so that the clojurian chaining macro ->> works well. The consistent argument order is as follows: procedure arguments appear first, lazy list arguments last. For example (Ref n Lst) replaces (list-ref lst n), (Drop n Lst) replaces (list-tail lst n), etc.

Storing the length in the list record has another advantage: One can check the type and finiteness of lazy-list arguments at the start of a routine. One could use the assert for that, but that would mean to always do all checks provided you don't compile in unsafe mode, which makes all code unsafe. So I provided another macro instead, assume-in, which does any error-checking only if a special feature, assumptions-checked, is registerd.

lazy-lists

[procedure](lazy-lists)

returns a sorted list of all exported symbols. Additional information of each method can be supplied by calls of method-assumptions and method-effects.

make-lazy

[procedure](Make-lazy len thunk)

lazy constructor. len is either a not-negative fixnum or #f for infinite Lists

input->List

For-each

lazy version of for-each. At least one of the Lsts must be finite. For-each terminates at its length.

Filter

[procedure](Filter ok? Lst)

lazy version of filter

Map

[procedure](Map proc . Lsts)

lazy version of map, terminates at the shortest Length.

Assoc

[procedure](Assoc key aLst)

lazy version of assoq

Assv

[procedure](Assv key aLst)

lazy version of assv

Assq

[procedure](Assq key aLst)

lazy version of assq

Assp

[procedure](Assp ok? aLst)

return #f or first pair, whose Car fulfills ok?

Equal?

[procedure](Equal? Lst1 Lst2)

lazy version of equal?

Eqv?

[procedure](Eqv? Lst1 Lst2)

lazy version of eqv?

Eq?

[procedure](Eq? Lst1 Lst2)

lazy version of eq?

Equ?

[procedure](Equ? =? Lst1 Lst2)

compare two Lists with predicate =?

Member

[procedure](Member var Lst)

lazy version of member

Memv

[procedure](Memv var Lst)

lazy version of memv

Memq

[procedure](Memq var Lst)

lazy version of memq

Memp

[procedure](Memp ok? Lst)

Tail of items not fulfilling ok?

Count-while

[procedure](Count-while ok? Lst)

return index of first item not fulfilling ok?

Drop-while

[procedure](Drop-while ok? Lst)

Tail of items not fulfilling ok? Lst must be finite.

Take-while

[procedure](Take-while ok? Lst)

List of items fulfilling ok? Lst must be finite.

Drop

[procedure](Drop n Lst)

lazy version of list-tail with changed argument order

Take

[procedure](Take n Lst)

List of first n items of Lst

List

[procedure](List . args)

lazy version of list. Constructs a finite List.

list->List

[procedure](list->List lst)

transform ordinary list into finite lazy list

List->list

[procedure](List->list Lst)

transform finite lazy into ordinary list

Realize

[procedure](Realize Lst)

realize a finite List

Realized?

[procedure](Realized? Lst)

Is Lst realized?

Primes

[procedure](Primes)

lazy list of prime numbers

Cardinals

[procedure](Cardinals)

lazy list of non negative integers

Interval

[procedure](Interval from upto)

List of integers from (included) upto (excluded)

assume-in

[syntax](assume-in sym test . tests)

Checks if all the assumptions test ... in the routine with name sym are valid and provides a meaningful error-message otherwise, provided the feature assumptions-checked is registered. Checks nothing if the feature is not registered.

Author

Initial version

Updated

License

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.