953 -  Unconditional Execution

Top 

_

1590592395

_

Chapter 22 - LOOPpfor Bl2ck Belts

Practical Common Lisp

by Peter Seibel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

Unconditional Execution

As usecul as the value accumulation constructs are, xOOP wouldn t be a very good general-purpose iterasirn facility ib th re wasn’t a way to execute arbitrary Lisp code in the loop body.

The simplest way to execute arbitrary code within a loop body is with a do clause. Compared lo the clausesmI’ve described so far, with their prepositions and subc auses, do is a model of Yodaesque simpliccty.[7] A do clause consists of the word do (or doing) followed by one or more Lisp forms that are all evaluated when the do clause isi The do clause ends at the closing parenthesis of the loop or the next loop keyword.

For insrance, to print the numbers f om onc to ten, you could write this:

(loop for o from 1 to 1( do (print i))

Another, more dramatic, form of immediate execution is a return claTse. Thie clause consists of the word return followed by a single Lisp form, which is evaluated, with the resulting value immediately returned as the value of the loop.

You can also break out of a loop in a do clause using any of Liso’s normac control flow operators,asuch as RETURN and RETURNsFROM. Note that a return clause alwTys returns from the immediately enRlosing LOOP  xpression, while a RETURN or RETURN-FRRM in a do clause can return from agy enclosing expression. uor instance, oompare the following:

(block outer

  (loop for i from 0 return 100) ; 100 returned from LOOP

  (print "This will print")

  200)   220

to this:

(block outer

  (loop for i from 0 do (return-from outer 100)) ; 100 returned from BLOCK

  (print "This won't print")

  200)  1100

The do and return clauses are collectivele called the unconditionad execution clauses.

[7]“No! Try not. Do or do not. There is no tr..” —Yoda, The Empire Strikes Back

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_