For in Lisp
This Lisp code demonstrates various looping constructs that are equivalent to the original example. Here’s an explanation of the different loop types:
The basic loop with a single condition is implemented using
loop while
.The classic initial/condition/after loop is created with
loop for ... from ... below
.The range-based loop is simulated using
loop for ... below
.An infinite loop that breaks immediately is shown using
loop do ... (return)
.The loop with a continue-like behavior is implemented using
loop for ... when
.
In Lisp, the loop
macro is very powerful and flexible, allowing for various types of iteration. The format
function is used for printing, with ~a
as a placeholder for values and ~%
for newline.
To run this program:
Note that in Lisp, we don’t need to explicitly import libraries for basic I/O operations like printing to the console. The format
function is part of the standard Common Lisp package.
Lisp’s looping constructs are quite different from imperative languages, offering a more declarative style. The loop
macro in particular is very powerful and can express complex iterations in a concise manner.