Common Lisp - Surgical Cuts With The Loop

CL Loop

The loop macro looks like noise to the untrained eye.
Parentheses stretch, clauses scatter, intentions get lost in plain sight.

Most devs avoid it, afraid it will swallow their code.
We don't fear.
We carve.

Each iteration becomes a precise strike, each clause a silent command.
In Common Lisp, loop is not chaos - it is the scalpel.

Wielded correctly, it cuts through repetition with surgical grace, leaving only what matters.

You are the Operator. You strike with loop.

Easily walk the elements of a list:

( loop for elem in '(1 2 3 4)
do (print elem))
 ;  ->
1
2
3
4
NIL

It returns NIL.

Ghosts sometimes want to return data:

( loop for elem in '(1 2 3 4)
collect (print elem))
 ;  ->
1
2
3
4
(1 2 3 4)

Or just return the interation's value as a list?

( loop for elem in '(1 2 3 4)
collect elem)
 ;  -> (1 2 3 4)

collect gathers the return value of the form.
print prints the iteration's value.

You may want to play with the data:

( loop for elem in '(1 2 3 4)
collect (+ 5 elem))
 ;  -> (6 7 8 9)

In cases Ghosts must break out. End the loop early:

( loop for x in '(1 2 3 4)
      when (= x 3) return x)
 ;  -> 3

Ghosts don't gather everything blindly.
Sometimes the strike is selective, surgical.
loop lets you collect only what matters.

( loop for x in '(1 2 3 4 5 6)
when (evenp x) collect x)
 ;  -> (2 4 6)

Only even numbers are harvested.
The rest vanish into silence.

Sometimes, the target must fall and the operation ends immediately.
Enter return:

( loop for x in '(1 2 3 4 5)
when (= x 3) return x)
 ;  -> 3

The moment the target appears, the loop stops.
No noise, no leftovers.

DeadSwitch principle:

collect = gather quietly, strike later.
return = strike now, mission complete.

Ghosts sometimes tally their victories.
loop provides counters, sums, and totals - silently tracking without clutter.

Count the enemies (or items) that meet your criteria:

( loop for x in '(1 2 3 4 5 6)
count (evenp x))
 ;  -> 3

Only the even targets are counted.
The rest fade into the shadows.

Sum their strength, silently, without printing a sound:

( loop for x in '(1 2 3 4 5 6)
sum x)
 ;  -> 21

The total emerges from the silence.
No intermediate noise.

Counters and sums can be combined with conditions, for surgical precision:

( loop for x in '(1 2 3 4 5 6)
when (evenp x) count x
when (oddp x) sum x)
 ;  -> 9

Even numbers are tallied: 2, 4, 6 → count = 3

Odd numbers are summed: 1 + 3 + 5 → sum = 9

DeadSwitch principle:

Track silently.
Strike selectively.
Let the data reveal the mission's outcome, not the code.

A Common Lisp community is building on Discord: https://discord.gg/eNvmFrSCNb

[ The Signal fades now. DeadSwitch is out. ]