5
votes

I have DrRacket Version 5.0.2, I've spent like 3 hours already looking for the right libraries to make while, dolist, and dotimes work. I know how to use them but I just can't find the right libraries. When I type dotimes for example it tells me unknown identifier.

PS: Do you have to use define-syntax in order to define these loops? I've tried (require srfi/42) but it does not work. I'm using #lang scheme.

3
while, dolist and dotimes are Common Lisp forms. You certainly could write macros for them in Scheme, but maybe you really want a Common Lisp environment instead?user725091
By the way, #lang scheme is a backwards compatibility language and is deprecated so you probably want to use #lang racket. Also, I suggest reading the guide as well (it's pretty good for these sorts of questions).Asumu Takikawa

3 Answers

9
votes

You could use Racket's built-in iteration forms instead:

Instead of (dolist (x some-list) body-forms ...), you can write (for ((x some-list)) body-forms ...)

Instead of (dotimes (i n) body-forms ...), you could use (for ((i (in-range 0 n))) body-forms ...) or even simply (for ((i n)) body-forms ...), as long as n is a non-negative integer.

You could write syntax-rules macros to transform the CL-style loops into Racket-style ones, but it's probably not worth it. Racket's for-forms are more flexible than dotimes or dolist by themselves, since you can easily use them to iterate over several sequences at once.

1
votes

There is also now an implementation of the Common Lisp loop macro for Racket. Import it like this:

(require (planet jphelps/loop))
0
votes

Number is actually a sequence.

> (sequence->list 5)
'(0 1 2 3 4)

(for ((i (in-range n))) body-forms ...) works too. in-range is faster.