1. simple-loops
    1. The routines
      1. simple-loops
      2. do-for
      3. do-forever
      4. do-list
      5. do-times
      6. do-until
      7. do-while
    2. Requirements
    3. Example
  2. Last update
  3. Author
  4. License
  5. Version History

simple-loops

This module exports some simple loop macros.

The routines

simple-loops

[procedure] (simple-loops [sym])

documentation-procedure; returns the list of exported symbols if called with no symbol, otherwise the signature of that exported symbol sym.

do-for

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

for-loop: do xpr . xprs for var in [start stop[ with steps (default 1)")

do-forever

[syntax] (do-forever xpr . xprs)

endless loop: executes body xpr . xprs until exit is called

do-list

[syntax] (do-list i lst xpr . xprs)

loop along a list: execute xpr . xprs for i in lst

do-times

[syntax] (do-times i upto xpr . xprs)

loops a fixed number of times: execute xpr . xprs for i in [0 upto[

do-until

[syntax] (do-until test? xpr . xprs)

until-loop: execute xpr . xprs while test? is false

do-while

[syntax] (do-while test? xpr . xprs)

while-loop: execute xpr . xprs while test? is true

Requirements

none

Example


(import simple-loops)

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

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

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

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

    (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 4) (lst '()))
      (do-while (<= 0 n)
        (set! lst (cons n lst))
        (set! n (- n 1)))
      lst)
    ;-> '(0 1 2 3 4)

Last update

Mar 17, 2019

Author

Juergen Lorenz

License

Copyright (c) 2013-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
port from chicken-4