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
          [optional-result-value])
  ...body... )
  
(do ((variable-1 
      variable-1-init-value 
      variable-1-update-expression)
     (variable-2 
      variable-2-init-value 
      variable-2-update-expression)
      ...
     (variable-N 
      variable-N-init-value 
      variable-N-update-expression))
  (loop-termination-test loop-return-value)
  optional-variable-declarations
  expressions-to-be-executed-inside-the-loop)

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.

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

The URI to TrackBack this entry is: https://burubaxair.wordpress.com/2013/03/25/dolist-dotimes-do-explained-mark-watson-loving-lisp/trackback/

RSS feed for comments on this post.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: