chickadee » list-comprehensions

List comprehensions

List comprehensions are popularized by other functional languages like Standard ML, Miranda, Haskell.

Here, it is implemented with the macro collect, which is inspired by Clojure's 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

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
repeat times xprocedure

produces a list of length times where all items are x

iterate-times

iterate-times fn kprocedure
iterate-times fn k startprocedure

produces a list where the items are constructed by successively applying fn k times to the last value, starting with start.

iterate-while

iterate-while fn ok?procedure
iterate-while fn ok? startprocedure

produces a list where the items are constructed by successively applying fn to the last value, starting with start, as long as the value passes the ok? test.

iterate-until

iterate-until fn ok?procedure
iterate-until fn ok? startprocedure

produces a list where the items are constructed by successively applying fn to the last value, starting with start, until the value passes the ok? test.

collect

(collect item qualifier ....)syntax

where item is an expression with variables to be bound in the qualifiers. Each qualifier is of the form (var lst fltr ...) whith

  • var a variable
  • lst a list from which values are chosen
  • fltr a filter expression

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

  (collect (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)

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

(collect (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)) 

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

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

Last update

Jul 05, 2019

Author

Juergen Lorenz

Repository

This egg is hosted on the CHICKEN Subversion repository:

https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/list-comprehensions

If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.

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.2
macro for renamed collect
1.1
curried and uncurried versions, syntax of for changed
1.0
initial import for chicken-5

Contents »