loop conformance

Pascal J. Bourguignon pjb at informatimago.com
Wed Jan 29 07:25:52 UTC 2014


Steve Haflich <shaflich at gmail.com> writes:

> I'm in a grumpy mood today, so I decided to take out my frustrations
> on the ANS for CL, or implementations thereof.  Consider carefully
> what the following form should return:
>
> (let ((v (make-array 10
>     :initial-contents '(0 1 2 3 4 5 6 7 8 9)
>     :fill-pointer 5)))
>   (loop for x across v
>       when (eql x 2) do (incf (fill-pointer v))
>       collect x))
>
> ACL and SBCL both return (0 1 2 3 4).  Probably every other
> implementation does too.  I believe the ANS requires the return to be
> (0 1 2 3 4 5), although the definition in 6.1.2.1.5 The for-as-across
> subclause uses various undefined terminology.  (We of X3J13
> understood that the loop specification was not our best work.)

http://www.lispworks.com/documentation/HyperSpec/Body/03_f.htm

    Array traversal

        For array traversal operations, the array is not allowed to be
        adjusted and its fill pointer, if any, is not allowed to be
        changed. 

So ISTM your code is not conforming, and the result (0 1 2 3 4) seems
reasonable to me, similarly to:

    (loop :with max = 10
          :for i :from 0 :below max
          :do (incf max)
          :collect i)
    --> (0 1 2 3 4 5 6 7 8 9)




Now, while we have:

    (loop :with l = (list 0 1 2 3 4 5 6 7 8 9)
          :for x :in l
          :when (eql x 2) do (setf (cdr (last l)) (list 10))
          :collect x)
    --> (0 1 2 3 4 5 6 7 8 9 10)

this is also clearly in contradiction to 3.6 therefore non-conforming
too.


-- 
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ?  C'est le moment d'acheter !"



More information about the pro mailing list