Subject: LOOP question

From: Wilhelm Burger <[email-masked]>
Date: Tue, 12 Sep 1995 16:13:10 +0200

Hi,
   I am having problems with the following loop construct which
evaluates differently in LUCID and CLISP:

;;;-------------------------------------------------
lucid>   (loop for i in '(1 2 3)
           for j = (1+ i)
           for k = (1- i)
           do (print (list j k))
           collect (* j k))
(2 0) 
(3 1) 
(4 2) 
(0 3 8)

;;;-------------------------------------------------
;;;-------------------------------------------------
clisp>   (loop for i in '(1 2 3)
           for j = (1+ i)
           for k = (1- i)
           do (print (list j k))
           collect (* j k))
(2 0) 
(3 1) 
(4 2) 
*** - argument to 1+ should be a number: NIL
1. Break> 
;;;-------------------------------------------------

Are the 2 "for"-bindings for j and k resp. supposed (allowed) to be
evaluated when the end of the list has already been reached (i.e., when
i is NIL)?

Regards,
Wilbur.

---
Wilhelm Burger, Johannes Kepler University, A-4040 Linz, Austria
Email:[email-masked]/Tel: +43 732 2468 898, Fax: 893

---

From: Marcus Daniels <[email-masked]>
Date: Tue, 12 Sep 1995 14:58:13 GMT

I agree that Lucid's behavior is more intuitive.  CMU Lisp also does
it this way:

(loop for i in '(1 2 3)
      for j = (1+ i)
      for k = (1- i)
      do (print (list j k))
      collect (* j k))
(2 0) (3 1) (4 2) (0 3 8)

instead of:
(loop for i in '(1 2 3)
      for j = (1+ i) for k = (1- i) 
      do (print (list j k))
      collect (* j k)) 
(2 0) (3 1) (4 2)
*** - argument to 1+ should be a number: NIL 
1. Break>

Can someone make an argument for keeping CLISP the way it is?
Anyway, here's a patch to change the behavior.  Let me know..

*** /a/marcus/loop.lsp	Sun Sep 10 03:38:55 1995
--- clisp/src/loop.lsp	Tue Sep 12 11:02:18 1995
***************
*** 590,599 ****
                                       (when step-function-var
                                         (push `(,step-function-var ,step-function-form) bindings)
                                       )
-                                      (push `(WHEN (ENDP ,var) (LOOP-FINISH)) stepbefore-code)
                                       (note-initialisation t nil
                                         'LET
!                                        (destructure pattern (if (eq preposition 'IN) `(CAR ,var) `,var))
                                         new-declspecs
                                       )
                                       (push
--- 590,602 ----
                                       (when step-function-var
                                         (push `(,step-function-var ,step-function-form) bindings)
                                       )
                                       (note-initialisation t nil
                                         'LET
!                                        (destructure pattern 
!                                          `(IF ,var
!                                            ,(if (eq preposition 'IN) `(CAR ,var) `,var)
!                                            (LOOP-FINISH)
!                                        )   )
                                         new-declspecs
                                       )
                                       (push
***************
*** 629,640 ****
                                         (index-var (gensym)))
                                     (push `(,vector-var ,vector-form) bindings)
                                     (push `(,index-var 0) bindings)
!                                    (push `(WHEN (>= ,index-var (LENGTH ,vector-var)) (LOOP-FINISH))
!                                          stepbefore-code
!                                    )
!                                    (note-initialisation t t
                                       'LET
!                                      (destructure pattern `(AREF ,vector-var ,index-var))
                                       new-declspecs
                                     )
                                     (push (list index-var `(1+ ,index-var)) stepafter)
--- 632,644 ----
                                         (index-var (gensym)))
                                     (push `(,vector-var ,vector-form) bindings)
                                     (push `(,index-var 0) bindings)
!                                    (note-initialisation t nil
                                       'LET
!                                      (destructure pattern
!                                        `(IF (>= ,index-var (LENGTH ,vector-var))
!                                          (LOOP-FINISH)
!                                          (AREF ,vector-var ,index-var)
!                                       ) )
                                       new-declspecs
                                     )
                                     (push (list index-var `(1+ ,index-var)) stepafter)