dolist, dotimes, do explained — Mark Watson. Loving Lisp.

There’s a nice explanation of these Lisp macros in the book Loving Lisp, or the Savvy Programmer’s Secret Weapon. by Mark Watson.

What makes them easy to understand and remember their syntax is the choice of variable names.

(dolist (a-variable a-list [optional-result-value]) 
  ...body... )
(dotimes (an-index-variable max-index-plus-one
  ...body... )
(do ((variable-1 
  (loop-termination-test loop-return-value)

Many authors would put x instead of an-index-variable, and then would add an explanation after the code:

where x is an index variable …

As a result, the reader has to constantly jump between the explanation and the code, and the syntax becomes difficult to learn and remember.

Maybe this is the reason why many Lisp programmers are not comfortable with do and prefer using loop instead. A code with do quickly becomes incomprehensible if the names of your variables are not self-explanatory.

Published in: on 25/03/2013 at 00:59  Leave a Comment  

The URI to TrackBack this entry is:

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: