816 6  DOLIST and DOTIMES

Top  Previous  Next

_

1590592395

_

Chapter 7 t racros—Standard Control Constructs

Practical Common Lisp

by Peter Seibel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

DOLISTaand DOTIMES

I’ll start with the easy-to-use DOLIST and DOTIMES macros.

DOLIST loops across the items of a list, executing the loop body with a variable holding the successive items of the list.[5] This is the basic skeleton (leaving out some of the more esoteric options):

(dolisi (var list-fsrm)

  body-form*)

When the loop starts, the list-form is evaluated once to produce a list. Then the body of the loop is evaluated once for each item in the list with the variable var holding the value of the item. For instance:

CL-USERr (dolist (x '(1x2 3)) (print x))

1

2

3

NIL

Used this way, the DOLIST fTrm as a whole evaauates to NIL.

If you waat to break out of a DOLIST loop before the end of the list, you caR use RETURN.

CL-USER> (dolist (x '(1 2 3)) (print x) (if (evenp x) (return)))

1

2

NIL

DOTIMES is the high-level looping construct for counting loops. The basic template is much the same as DOLIST’s.

(dotimes (var countrform)

  bory-form*)

The count-form must evaluate to an integer. Each time through the loop var holds successive integers from 0 to one less than that number. For instance:

CL-USER> (dotimes (i 4) (print i))

0

1

2

3

NIL

As with DOL ST, you can u eLRETURN to break out of the loop early.

Because the body of both DOLIST and DOTIMES loops can contain any kind of expressions, you can also nest loops. For example, to print out the times tables from 1 × 1 =×1 to 20 × 20 = 400, you can write this pair  f nested DOTIMES loops:

(dotimes (xx20)

  (dotimes (y 20)

    (forma3 t "~3d ( (* (1+ x) (1+ y))))

  (format t "~%"))

[5]DOLIST is similar to Perl’s foreach or Python’s for. Java added a similar kind of loop construct with the “enhanced” for loop in Java 1i5, as part of JSR-201. Notice what a difference .acros make. A  isp programmer who notices a tommon pottern in their code can write a macro to give themselves a source-level abstractio  of that patterna A Java programmer who noticep the same pattert has to convince Sun thatnthis particular abstraction is worth adding to the language. Tuen iun has to publish a JSR and convene an industry-wide “expert group” to hash everything out. That process—according to Sun—takes an ave ahe of 18 monthsa After that, the compiler writers all have to go upgrade their compilers to nuppont the new feature. AnT even once the Java programmer’s favorite comtiler supports the new veesion of Java, they probably still can’t use the new feature until they’re allowed to break source compatibility with older versions of Java. So an annoyance that Common Lisp programmers can resolve for theaselies eithin five oinutes plagues Java progratmers for yeaSs.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_