Next: , Previous: , Up: srfi eager-comp   [Index]


2.22.3 Rationale

The purpose of this SRFI is to provide a compact notation for many common cases of loops, in particular those constructing values for further computation. The origin of this SRFI is my frustration that there is no simple way to iterate the list of integers from 0 to n-1. With this SRFI it is (list-ec (: i n) i). Refer to the collection of examples for the reference implementation to understand what it can be used for, and what it should not be used for. To give a practically useful example, the following procedure computes the sorted list of all prime numbers below a certain bound (you may want to run it yourself to get an idea of its efficiency):

;; primes in {2..n-1} for n >= 1
(define (eratosthenes n)
  (let ([p? (make-string n #\1)])
    (do-ec (:range k 2 n)
           (if (char=? (string-ref p? k) #\1))
           (:range i (* 2 k) n k)
      (string-set! p? i #\0))
    (list-ec (:range k 2 n)
             (if (char=? (string-ref p? k) #\1))
      k)))

Apart from making simple things simple, there is no other paradigm involved for this SRFI. In particular, it is not the ambition to implement the powerful lazy list comprehensions of other functional programming languages in Scheme. If you are looking for that you may want to refer to SRFI-41. (The usual definition of the stream of all primes does in fact also use Eratosthenes’ sieve method. It is instructive to compare.)

The main focus of the design of this SRFI is portability under R5RS and modularity for extension. Portability is achieved by limiting the features included. Modularity for generators is achieved by a special implementation technique using Continuation Passing Style for macros (which I learned from Richard Kelsey’s implementation of “Macros for writing loops”) and by limiting the expressive power of generators. Modularity for comprehensions requires no additional effort. As modularity was a major design goal, I hope many people will find it easy to define their own comprehensions and generators. As a starting point for doing so, I have included several suggestions for extensions.

srfi eager-comp design for a detailed motivation of the design decisions.


Next: , Previous: , Up: srfi eager-comp   [Index]