Hi,
I stumbled across the following:
(loop for l on '(a b c d) by #'cddr do (prin1 l) finally (print l))
(A B C D)(C D)
NIL ; <- in finally
NIL
(loop for (l) on '(a b c d) by #'cddr do (prin1 l) finally (print l))
AC
C ; <- in finally
NIL
In the first loop above, the final value (NIL) is that computed after
the last step, where the list end is detected. L has already been
assigned (a third time). In the second loop, the final value (C) is
that from the last iteration through the body, L has not been assigned
(or destructured) a third time.
Is that necessarily so?
I read the CLHS chapters on loop again and again, and I'm less and
less convinced that there are guarantees about the value of variables
inside finally. Especially, stepping and end-test seems to me like
they can occur in any order, since the cases for internal and external
(programmer visible) variables may be distinct.
So my conclusion is that the first loop above need not necessarily
exhibit l<-NIL in finally. Another correct loop implementation might
be:
(let* ((internal '(a b c d))
(l internal))
(tagbody ; block nil omited
(go test)
body (prin1 l)
step1 (setf internal (cddr internal))
test (when (endp internal) (go finally))
step2 (setf l internal)
(go body)
finally (print l)))
That would not print l->NIL in finally.
What would you think?
Jorg Hohle
Telekom/T-Systems Technology Center
|
|
0
|
|
|
|
Reply
|
hoehle5022 (185)
|
5/13/2005 3:51:23 PM |
|
Joerg Hoehle wrote:
> Hi,
>
> I stumbled across the following:
>
> (loop for l on '(a b c d) by #'cddr do (prin1 l) finally (print l))
> (A B C D)(C D)
> NIL ; <- in finally
> NIL
>
> (loop for (l) on '(a b c d) by #'cddr do (prin1 l) finally (print l))
> AC
> C ; <- in finally
> NIL
Well, here you used "for l ON ...", not IN, so in the first l gets
bound to all the cons cells while iterating: (a . ...) (c . ...)
and nil. In the second loop you have a destructuring value, so l
gets bound to only the CAR of each cons cell. I think the second
is equivalent to
(loop for l in ...), but I'm not sure.
> In the first loop above, the final value (NIL) is that computed after
> the last step, where the list end is detected. L has already been
> assigned (a third time). In the second loop, the final value (C) is
> that from the last iteration through the body, L has not been assigned
> (or destructured) a third time.
>
> Is that necessarily so?
The form "(l)" (eqv to (l . nil)) does not match nil in the last
iteration, because nil isn't a cons cell, so the second loop can't
iterate another time.
But why aren't you just using "in"?
What are you trying to do?
--
Don't let school interfere with your education. -- Mark Twain
|
|
0
|
|
|
|
Reply
|
u.hobelmann (1637)
|
5/13/2005 5:09:21 PM
|
|
Ulrich Hobelmann <u.hobelmann@web.de> writes:
> The form "(l)" (eqv to (l . nil)) does not match nil in the last
> iteration, because nil isn't a cons cell, so the second loop can't
> iterate another time.
Are you implying that the destructuring pattern affects how many
times a for-as-on-list clause will iterate?
* (loop for () on '(a b c d e) count t)
5
* (loop for (x y z) on '(a b c d e) count t)
5
|
|
0
|
|
|
|
Reply
|
kon (424)
|
5/13/2005 10:12:53 PM
|
|
Kalle Olavi Niemitalo wrote:
> Ulrich Hobelmann <u.hobelmann@web.de> writes:
>
>
>>The form "(l)" (eqv to (l . nil)) does not match nil in the last
>>iteration, because nil isn't a cons cell, so the second loop can't
>>iterate another time.
>
>
> Are you implying that the destructuring pattern affects how many
> times a for-as-on-list clause will iterate?
>
> * (loop for () on '(a b c d e) count t)
>
> 5
> * (loop for (x y z) on '(a b c d e) count t)
>
> 5
Hm, then why did OP's example only iterate one less time with "(l)
on", while the "l in" CDDRed through the list and found nil at the
end? I'm clueless again.
--
Don't let school interfere with your education. -- Mark Twain
|
|
0
|
|
|
|
Reply
|
u.hobelmann (1637)
|
5/16/2005 8:58:36 PM
|
|
Ulrich Hobelmann <u.hobelmann@web.de> writes:
> Kalle Olavi Niemitalo wrote:
>> * (loop for () on '(a b c d e) count t)
>> 5
>> * (loop for (x y z) on '(a b c d e) count t)
>> 5
> Hm, then why did OP's example only iterate one less time with "(l)
> on", while the "l in" CDDRed through the list and found nil at the
> end? I'm clueless again.
Huh, both examples looped the same number of times: 2
> What are you trying to do?
I came across the following example:
(defun my-memberl (elt list &key (test #'eql) (key #'identity))
(loop for l on list
until (funcall test elt (funcall key (car l)))
finally (return l)))
It struck me, because I'd have never written the loop this way.
I believe Antonio Menezes Leitao wrote it this way on one of his
Amsterdam 2005 meeting slides.
I question the validity of that code, since I'm not convinced that l
is nil after the list is exhausted.
This argument is based on the following possible (agreed naive)
implementation of loop for ... on:
(loop for internal on '(a b c d) by #'cddr
for l = internal ; for (l1 l2 l3) = internal with destructuring
do (prin1 l)
finally (print l))
(A B C D)(C D)
(C D) ; l in finally
NIL
Written that way, l is not nil inside the finally clause, unlike the
for l on ... case (at least in cmucl, sbcl and clisp).
This version makes for l on ... appear as a special-case optimization
on the more general destructuring for <pattern> on ... case, where the
internal and single user-given variables can be identical.
For example, the Iterate package currently behaves differently (it
effectively uses that internal variable). Thus, if I can be convinced
that CLHS mandates NIL for l in the finally section, I'll go and
change Iterate's behaviour.
Regards,
Joerg Hoehle.
|
|
0
|
|
|
|
Reply
|
hoehle5022 (185)
|
5/20/2005 1:30:43 PM
|
|
|
4 Replies
34 Views
(page loaded in 0.098 seconds)
Similiar Articles: set indexing( i+1) as a variable - comp.soft-sys.matlabA = 1:5 for i = 1 : 3 %this for loop assigns a new value to "i ... an extra-cost for all the other license forms. ... for loop variable scope - comp.lang.tcl If that is empty ... Creating Variables thru loop or ? - comp.soft-sys.matlab ...> Hello All, > Problem: > Need to extract values ranging from index 1 to 5 and ... how to use for-loop variable in a filename - comp.soft-sys.matlab ... Creating Variables ... Plotting multiple variables generated from a loop in the same ...... trying to plot a set of datas generated from a loop on ... that the data is generated like this Freq (fixed value ... Tt is trivial to handle the data set on this form. Another PARFOR reduction variable headache - comp.soft-sys.matlab ...My problem is of the form: A = ones(10 ... find(isnan(X)); % find missing values [jmis ... by using parfor ... name in a FOR loop - comp.soft-sys.matlab ... variable ... Compare 5 variables' values - comp.soft-sys.sasI need to compare 5 variables' values p1 -p5 , and create a new variable ... to get this rather than writing a loop ... to create expressions that compare values. ... Finally ... access variable by string name - comp.soft-sys.matlabchanging the variable name in a FOR loop - comp.soft-sys ... While( valstem <> "") Parse Value valstem ... access variable ... set the form action variable var MM_editAction ... Geting Checkboxlist value in javascript - comp.lang.javascript ...... am having a checkboxlist..now how do i get the value of ... or a single element Here's some play code: <form ... New with Javascript: Who like to help me with a loop ... instead of For loop - comp.soft-sys.matlabThere is no general closed-form formula ... comp.soft-sys.matlab changing the variable name in a FOR loop ... everyone, I have created a simple for loop where the values ... Reading ASCII text file with variable number of columns - comp ...... all-string, tab-delimited ASCII text file of the form ... read in, I would like to set "Xaxis" equal to a variable ... way you go, whether you choose NaN for missing values or ... Passing data from one form to another - comp.lang.pascal.delphi ...Form2.ShowModal; finally Form2 ... See what I did for the string variable. > In Form2 (i:e. the form that ... about how to pass control's values from one form to ... LOOP for Black Belts - gigamonkeysIf the with clause contains an = value-form part, the variable will be initialized, before ... loop variables are initialized and before the body of the loop. The finally forms ... 26.8. Value Accumulation - SCHOOL OF COMPUTER SCIENCE, Carnegie MellonI happen to prefer the non-``ing'' forms-when I use loop at all ... does not provide a default return value; however, the variable is available for use in any finally clause. 7/23/2012 2:57:18 AM
|