David N. Welton <davidw@cks.com> wrote:
+---------------
| I suppose.. although from what I have seen of the contortions
| necessary to emulate a 'while' loop...
+---------------
What, you mean this isn't simple enough for you? ;-} ;-}
> (defmacro while (pred . body)
`(do () ((not ,pred)) ,@body))
or, if you prefer R5RS-style macros:
> (define-syntax while
(syntax-rules ()
((while pred) ; allow the empty-body case
(do () ((not pred))))
((while pred body ...)
(do () ((not pred)) body ...))))
and in either case:
> (define x 3)
> (while (> x 0)
(display x)
(newline)
(set! x (- x 1)))
3
2
1
>
But I agree with the other poster: After you've used it a bit,
Scheme's "do" is usually a cleaner way to do stuff, e.g.:
> (do ((x 3 (- x 1)))
((<= x 0))
(display x)
(newline))
3
2
1
>
Or even named-LET:
> (let loop ((x 3))
(when (> x 0)
(display x)
(newline)
(loop (- x 1))))
3
2
1
>
-Rob
-----
Rob Warnock, 7L-551 rpw3@sgi.com http://reality.sgi.com/rpw3/
Silicon Graphics, Inc. Phone: 650-933-1673
2011 N. Shoreline Blvd. FAX: none (temp)
Mountain View, CA 94043 PP-ASEL-IA