chickadee » loops

loops

This module contains some easy looping macros. The documentation is included in the module, i.e. there is a dispatcher routine with the modules name, loops, which when called without arguments returns a list of the exported symbols, and when called with such a symbol prints that symbols documentation.

(loops)

returns the list of exported symbols, namely

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

shows the documentation of do-list, i.e. something like

loop along a list
(do-list var lst xpr . xpr)
executes xpr . xps for var in lst.

Programming interface

do-for

(do-for var (start stop [step]) xpr . xprs) syntax

where step is optional and defaults to 1. Executes xpr . xprs for var in [start stop[ with steps step.

do-forever

(do-forever xpr . xprs) syntax

executes xpr . xprs until exit is called.

This macro is unhygienic by design. It exports the symbol exit into the macro's scope.

do-list

(do-list var lst xpr . xprs) syntax

executes xpr . xprs for var in a list lst.

do-times

(do-times var upto xpr0 . xprs) syntax

executes xpr . xprs for var in [0 upto[

do-until

(do-until test? xpr . xprs) syntax

executes xpr . xprs while test? is false

do-while

(do-while test? xpr . xprs) syntax

executes xpr . xprs while test? is true

Usage

(use loops)

Examples

(use 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

None

Last update

Feb 15, 2014

Author

Juergen Lorenz

License

Copyright (c) 2011-2014, 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.4
tests ported to simple-tests
1.0
dependency on contracts removed
0.2
dependency removed
0.1
initial import

Contents »