2

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:

Charles Goodwin
  • 6,402
  • 3
  • 34
  • 63
toxaus
  • 23
  • 4

3 Answers3

1

(defun foo-loop (n) (cond ((zp n) "done") (t (prog2$ (cw "~x0" n) (foo-loop (1- n)))))

(foo-loop 10)

You can redo the termination condition and the recursion to mimic going from 1 to 10.

interestedparty333
  • 2,386
  • 1
  • 21
  • 35
1

A solution that uses recursion:

> (defun for-loop (from to fn)
    (if (<= from to)
       (progn
         (funcall fn from)
         (for-loop (+ from 1) to fn))))

;; Test
> (for-loop 1 10 #'(lambda (i) (format t "~a~%" i)))
1
2
3
4
5
6
7
8
9
10
NIL
Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93
  • ACL2 Error in ( DEFUN FOR-LOOP ...): IF takes 3 arguments but in the call (IF (<= FROM TO) (PROGN (FUNCALL FN FROM) (FOR-LOOP (+ FROM 1) TO FN))) it is given 2 arguments. The formal parameters list for IF is (X Y Z). Its prints by ACL2 system. When i try add third arguments to IF (just 'nil to the end ) – toxaus Jan 21 '11 at 07:10
  • its return - ACL2 p!>(defun for-loop (from to fn) (if (<= from to) (progn (funcall fn from) (for-loop (+ from 1) to fn)) 'nil) ) ACL2 Error in ( DEFUN FOR-LOOP ...): We do not permit the use of PROGN inside of code to be executed by Common Lisp because its Common Lisp meaning differs from its ACL2 meaning. – toxaus Jan 21 '11 at 07:12
1

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.
             .
            .
           .)))
Gareth Rees
  • 64,967
  • 9
  • 133
  • 163