Search code examples
lispcommon-lispacl2

How to loop using recursion in ACL2?


I need to make something like this but in ACL2:

for (i=1; i<10; i++) {
    print i;
}

It uses COMMON LISP, but I haven't any idea how to do this task...

We can't use standard Common Lisp constructions such as LOOP, DO. Just recursion.

I have some links, but I find it very difficult to understand:


Solution

  • The section "Visiting all the natural numbers from n to 0" in A Gentle Introduction to ACL2 Programming explains how to do it.

    In your case you want to visit numbers in ascending order, so your code should look something like this:

    (defun visit (n max ...)
      (cond ((> n max) ...)             ; N exceeds MAX: nothing to do.
            (t .                        ; N less than or equal to MAX:
                . n                     ; do something with N, and
                 .
                  (visit (+ n 1) max ...) ; visit the numbers above it.
                 .
                .
               .)))