You are looking at historical revision 37753 of this page. It may differ significantly from its current revision.

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.

Other list-generating procedures, like range, repeat, iterate-times, iterate-until and iterate-while are provided as well, in a curried and an uncurried version. In the sequel, only the uncurried version is documented, the curried version is supplied to be used with map and friends.

The API

list-comprehensions

[procedure] (list-comprehensions sym ..)

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

range

[procedure] (range upto)
[procedure] (range from upto)
[procedure] (range from upto step)

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

repeat

[procedure] (repeat times)
[procedure] (repeat times x)

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

iterate-times

[procedure] (iterate-times fn times)
[procedure] (iterate-times fn times start)

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

iterate-while

[procedure] (iterate-while fn ok?)
[procedure] (iterate-while fn ok? start)

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

iterate-until

[procedure] (iterate-until fn ok?)
[procedure] (iterate-until fn ok? start)

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

for

</macro>(for item qualifier ....)</macro>

where each qualifier is of the form (var lst fltr ...) whith

Creates a new list by binding var to each element of lst in sequence, and if it passes the checks fltr ..., inserts the item expression into the result list. The qualifieres are processed sequentially from left to right, so that filters of a qualifier have access to the variables of qualifiers to its left.

For example

  (for (list x y z)
       (x (list #\A #\B))
       (y (list 1 2))
       (z (list #f #t)))

produces

  ((#\A 1 #f)
   (#\A 1 #t)
   (#\A 2 #f)
   (#\A 2 #t)
   (#\B 1 #f)
   (#\B 1 #t)
   (#\B 2 #f)
   (#\B 2 #t))

Requirements

None

Examples


(import 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 (list c k)
     (c (string->list "ABCDEFG"))
     (k '(1 2 3 4 5 6 7 8)))
; -> ((#\A 1) ... (#\A 8) ... (#\G 1) ... (#\G 8))

(for (list c k)
     (c (string->list "ABCDEFG"))
     (k '(1 2 3 4 5 6 7 8) (memv c '(#\A #\G)) (memv k '(1 8))))
; -> ((#\A 1) (#\A 8) (#\G 1) (#\G 8)) 

(for (add1 x) (x '(0 1 2 3))) ; map
; -> '(1 2 3 4)

(for x (x '(0 1 2 3 4 5) (odd? x))) ; filter
; -> '(1 3 5)

Last update

Jul 04, 2019

Author

Juergen Lorenz

License

Copyright (c) 2017-2019, 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

1.1
curried and uncurried versions, syntax of for changed
1.0
initial import for chicken-5