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

loops

This module contains some easy looping macros. They are implemented in the Design by Contract style, i.e. using the contracts module. A corollary of this is, that the documentation is included in the module. By convention, there is a routine with the module's name, loops, which when called with no argument

(loops)

returns the list of exported symbols, namely

(do-for do-forever do-list do-times do-until do-while)

which are all macros, while calling loops with one of this symbols, e.g.

(loops 'do-list)

shows the documentation of this symbol in all it's glory, i.e. together with admissible forms and documentation

(forms: (do-list i lst xpr xpr1 ...))
"do xpr ... for i in a list lst"

Another way to get the complete documentation is to use print-doclist from the contracts module. Issuing

(import loops (only contracts print-doclist))
(with-output-to-file "loops.docu" (lambda () (print-doclist)))

you'll get the complete documentation written to the file loops.docu, which looks as follows:

do-for
------
(forms:
	(do-for var (start stop step) xpr xpr1 ...)
	(do-for var (start stop) xpr . xprs))
"do xpr ... for var in [start stop[ with steps (default 1)"
 
do-forever
----------
(forms: (do-forever . body) "executes body until exit is called")
   
do-list
-------
(forms: (do-list i lst xpr xpr1 ...))
"do xpr ... for i in a list lst"
   
do-times
--------
(forms: (do-times i upto xpr0 xpr1 ...))
"do xpr ... for i in [0 upto["
   
do-until
--------
(forms: (do-until test? xpr xpr1 ...))
"do xpr ... while test? is false"
   
do-while
--------
(forms: (do-while test? xpr xpr1 ...))
"do xpr ... while test? is true"

Usage

(import loops)

Examples

(import loops)

(let ((lst '()))
	(do-times i (+ 2 3)
		(set! lst (cons i lst)))
	lst) ; -> '(4 3 2 1 0)

(let ((lst '()))
	(do-list i '(1 2 3) (set! lst (cons i lst)))
	lst) ; -> '(3 2 1)

(let ((lst '()))
	(do-for i (1 5) (set! lst (cons i lst)))
	(reverse lst)) ; -> '(1 2 3 4)

(let ((lst '()))
	(do-for i (1 65 i) (set! lst (cons i lst)))
	(reverse lst)) ; -> '(1 2 4 8 16 32 64)

(let ((n 4) (lst '()))
	(do-while (<= 0 n)
		(set! lst (cons n lst))
		(set! n (- n 1)))
	lst) ; -> '(0 1 2 3 4)

(let ((n 4) (lst '()))
	(do-until (> 0 n)
		(set! lst (cons n lst))
		(set! n (- n 1)))
	lst) ; -> '(0 1 2 3 4)

(let ((n 3) (lst '()))
	(do-forever
		(if (zero? n) (exit lst))
		(set! lst (cons 'a lst))
		(set! n (- n 1)))) ; -> '(a a a)

Requirements

contracts

Author

Juergen Lorenz

License

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