1. List comprehensions
    1. The API
      1. list-comprehensions
      2. range
      3. repeat
      4. iterate-times
      5. iterate-while
      6. iterate-until
      7. for
    2. Requirements
    3. Examples
  2. Last update
  3. Author
  4. License
  5. Version History

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.

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)

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

iterate-times

[procedure] (iterate-times fn times)

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

iterate-while

[procedure] (iterate-while fn ok?)

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?)

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

for

[syntax] (for (qualifier ....) item)

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 ((x (list #\A #\B))
        (y (list 1 2))
        (z (list #f #t)))
       (list x y z))

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

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

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

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

Last update

Jan 28, 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.0
initial import for chicken-5