[plt-scheme] Do Scheme programmers use the 'do' form very often?

From: Doug Orleans (dougorleans at gmail.com)
Date: Thu Feb 14 10:18:52 EST 2008

Grant Rettke writes:
 > Do Scheme programmers use the 'do' form very often?

I use it sometimes.  Here's a recent usage (variable names changed to
protect the innocent):

    (do ((n 1 (add1 n)) (l l (cdr l)))
        ((null? l))
      (let ((x (car l)))
        ;; code involving n and x that is evaluated for side-effect,
        ;; e.g. printing, or updating a database
        ...))

This seems nicer than constructing a list of 1..(length l) and using
for-each, but that's a matter of taste.  Probably there's some more
elegant way to do it using iterations/comprehensions but I haven't
delved into that part of the new manual yet.

...Wait, okay, I just did, and this is how I'd do it now:
    
    (for ((n (in-naturals 1)) (x l))
      ...)

Learn something new every day!

--dougorleans at gmail.com


Posted on the users mailing list.