17.05.2015 Views

zl:1 - FTP

zl:1 - FTP

zl:1 - FTP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

361 nconc<br />

nconc could have been defined by:<br />

nconc Keyword For loop<br />

nconc expr {into var}<br />

(defun nconc (x y)<br />

;for simplicity, this definition<br />

(cond «null x) y)<br />

;only works for 2 arguments.<br />

(t (rplacd (last x) y) ;hook y onto x<br />

x») ;and return the modified x.<br />

I<br />

Causes the values of expr on each iteration to be nconced together, for example:<br />

(loop for i from 1 to 3<br />

nconc (list i (* i i»)<br />

=> (1 1 2 4 3 9)<br />

When the epilogue of the loop is reached, var has been set to the accumulated<br />

result and can be used by the epilogue code.<br />

It is safe to reference the values in var during the loop, but they should not be<br />

modified until the epilogue code for the loop is reached.<br />

The forms nconc and nconcing are synonymous.<br />

Examples:<br />

(de fun indexing (small-list)<br />

(loop for x from 0<br />

for item in small-list<br />

nconc (list x item») => INDEXING<br />

(indexing '(a b cd» => (0 A 1 B 2 C 3 D)<br />

Is equivalent to<br />

(defun indexing (small-list)<br />

(loop for x from 0<br />

for item in small-list<br />

nconcing (list x item») => INDEXING<br />

(indexing '(a b cd» => (0 A 1 B 2 C 3 D)<br />

Not only can there be mUltiple accumulations in a loop, but a single accumulation<br />

can come from multiple places within the same loop form, if the types of the<br />

collections are compatible. nconc, collect, and append are compatible.<br />

See the section "loop Clauses", page 310.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!