chickadee » list-comprehensions

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

List comprehensions

List comprehensions are popularized by other functional languages like Standard ML, Miranda, Haskell. The implementation of this module is inspired by Clojure, in particular the for macro. But it is provided in two versions, a parallel one, for, and a sequential one, for*. But while Clojure iterates over lazy sequences, I prefer iterator expressions instead.

Other list-generating procedures, like range, repeat, iterate-times, iterate-until and iterate-while are provided as well.

The API

list-comprehensions

list-comprehensions sym ..procedure

Documentation procedure. Without argument, returns the list of exported symbols, with argument the documentation of that symbol.

range

range uptoprocedure
range from uptoprocedure
range from upto stepprocedure

creates a list of numbers with given limits from defaults to 0 step defaults to 1

repeat

repeat timesprocedure

returns a unary procedure which repeats its only argument a number of times

iterate-times

iterate-times fn timesprocedure

returns a unary procedure which iterates the function fn on its only argument a number of times

iterate-while

iterate-while fn ok?procedure

returns a unary procedure which iterates the function fn on its only argument while the predicate ok? returns true

iterate-until

iterate-until fn ok?procedure

returns a unary procedure which iterates the function fn on its only argument until the predicate ok? returns true

for

</macro>(for (iterator iterator ...) xpr ....)</macro>

where each iterator is of the form

 (var start next stop when ...)

whith

Creates a list from variables var ... iterating in parallel with values generated on each iteration from xpr ....

for*

</macro>(for* (iterator iterator ...) xpr ....)</macro>

the same as for, but iterating variables sequentially where the further on the right the variable, the faster the iteration.

Requirements

None

Examples

(use list-comprehensions)

(range 0) ; -> '()

(range 5) ; -> '(0 1 2 3 4)

(range -5) ; -> '(0 -1 -2 -3 -4)

(range 1 5) ; -> '(1 2 3 4)

(range 5 1) ; -> '(5 4 3 2)

(range 1 5 2) ; -> '(1 3)

((repeat 5) 'x) ; -> '(x x x x x)

((iterate-times add1 5) 1) ; -> '(1 2 3 4 5)

((iterate-until sub1 zero?) 5) ; -> '(5 4 3 2 1)

((iterate-while sub1 positive?) 5) ; -> '(5 4 3 2 1)

(for ((x 1 (add1 x) (>= x 10) (odd? x))) x)
  ; -> '(1 3 5 7 9)

(for ((x 1 (add1 x) (>= x 10) (odd? x))
      (y 2 (add1 y) #t (even? y)))
  (cons x y))
  ; -> '((1 . 2) (3 . 4) (5 . 6) (7 . 8) (9 . 10))

(for* ((x 1 (add1 x) (>= x 8) (odd? x))
       (y 2 (add1 y) (>= y x) (even? y)))
  (cons x y))
  ; -> '((3 . 2) (5 . 2) (5 . 4) (7 . 2) (7 . 4) (7 . 6))

(for ((x 0 (add1 x) #t (odd? x))
      (y 1 (add1 y) (>= y 8) (even? y)))
  (let ((a (* x x)) (b (+ y y)))
    (list (cons x a) (cons y b))))
  ; -> '(((1 . 1) (2 . 4))
         ((3 . 9) (4 . 8))
         ((5 . 25) (6 . 12)))

(for ((x (range 10) (cdr x) (null? x) (or (null? x) (odd? (car x)))))
  (car x))
  ; -> '(1 3 5 7 9)

(let* ((lst (range 1 7))
       (len (length lst)))
  (for ((k 0 (add1 k) (>= k len) (even? k)))
    (let ((val (list-ref lst k)))
      (* 2 val))))
  ; -> '(2 6 10)

(let* ((vec (vector 0 1 2 3 4 5 6))
       (len (vector-length vec)))
  (for ((k 0 (add1 k) (>= k len) (even? k)))
    (let ((val (vector-ref vec k)))
      (* val val))))
  ; -> '(0 4 16 36)

Last update

Jan 12, 2017

Author

Juergen Lorenz

License

Copyright (c) 2017, Juergen Lorenz
All rights reserved.
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.

Version History

0.1
initial import

Contents »