For in Lisp

(defun main ()
  ;; The most basic type, with a single condition.
  (let ((i 1))
    (loop while (<= i 3)
          do (format t "~a~%" i)
             (incf i)))

  ;; A classic initial/condition/after loop.
  (loop for j from 0 below 3
        do (format t "~a~%" j))

  ;; Another way of accomplishing the basic "do this N times" iteration.
  (loop for i below 3
        do (format t "range ~a~%" i))

  ;; Loop without a condition will repeat until break.
  (loop do (format t "loop~%")
        (return))

  ;; You can also continue to the next iteration of the loop.
  (loop for n below 6
        when (oddp n)
        do (format t "~a~%" n)))

(main)

This Lisp code demonstrates various looping constructs that are equivalent to the original example. Here’s an explanation of the different loop types:

  1. The basic loop with a single condition is implemented using loop while.

  2. The classic initial/condition/after loop is created with loop for ... from ... below.

  3. The range-based loop is simulated using loop for ... below.

  4. An infinite loop that breaks immediately is shown using loop do ... (return).

  5. 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:

$ sbcl --script for.lisp
1
2
3
0
1
2
range 0
range 1
range 2
loop
1
3
5

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.