DEV Community 👩‍💻👨‍💻

Vee Satayamas
Vee Satayamas

Posted on

C-style for loop in Common Lisp's Loop macro

I try to convert C-style for-loop in JS to Common Lisp's loop macro for a learning purpose.

for (i=0; i<10; i++) {
  console.log(i)
}
Enter fullscreen mode Exit fullscreen mode

Above JS code can be converted to:

(loop with i = 0
      unless (< i 10) return nil
      do (print i)
      do (incf i))
Enter fullscreen mode Exit fullscreen mode

And, of course, the version below is more appropriate.

(loop for i from 0 below 10 do
    (print i))
Enter fullscreen mode Exit fullscreen mode

The first version looks more flexible in applying to another problem than the last one.

Top comments (0)

50 CLI Tools You Can't Live Without

The top 50 must-have CLI tools, including some scripts to help you automate the installation and updating of these tools on various systems/distros.