CL-USER 1 > (loop for i for j in '(a b c) collect (cons i j))
=> ((0 . A) (1 . B) (2 . C))
works perfectly on LispWorks, but not with SBCL
=> FOR is an unknown keyword in FOR or AS clause in LOOP.
I found a way around it 'for i from 0' but am not sure if there is
another smarter way to achieve this.
Jens
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
I think the canonical way would be
(loop for i upfrom 0
for j in '(a b c)
collect (cons i j))
ciao,
Jochen
> This ...
> CL-USER 1 > (loop for i for j in '(a b c) collect (cons i j))
> => ((0 . A) (1 . B) (2 . C))
> works perfectly on LispWorks, but not with SBCL
> => FOR is an unknown keyword in FOR or AS clause in LOOP.
It looks like LispWorks is being more tolerant than it needs to be.
According to the CLHS[1], you need to have at least one preposition
after the variable name in arithmetic stepping clauses.
> I found a way around it 'for i from 0' but am not sure if there is
> another smarter way to achieve this.
That's probably the easiest way to go. There are dozens of others,
though. For example:
(let ((count 0))
(mapcar (lambda (elt)
(prog1
(cons count elt)
(incf count)))
'(a b c)))
Cheers,
Pillsy
Indeed take for example :
LW
CL-USER 1 > (loop for i downto -10 collect i)
(0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10)
& Allegro
CG-USER(1): (loop for i downto -10 collect i)
Error: Don't know where to start stepping.
Current LOOP context: FOR I DOWNTO -10 COLLECT.
[condition type: PROGRAM-ERROR]
Makes your life easier but non-portable better avoid such things.
JT> CL-USER 1 > (loop for i for j in '(a b c) collect (cons i j))
JT> => ((0 . A) (1 . B) (2 . C))
and, do you really like that code? is it readable?
if I encounter such stuff, i'd yell "WTF??".
JT> I found a way around it 'for i from 0'
as others have noted, canonic way is to use upfrom. for i upfrom 0.
perfectly readable and makes sense for anybody, even if they do not know the
language.
JT> if there is another smarter way to achieve this.
what are you going to achieve, unreadable code? try DO then.
)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"scorn")
Opinions on what is and isn't "lispy" vary enormously.
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
"I saw the code for your computer program yesterday. It looked easy.
Its just a bunch of typing. And half of the words were spelt wrong.
And dont get me started on your over-use of parens."
- The Pointy Haired Boss sees some actual code
LOL
I find the OP's suggestion paradoxical: he is suggesting ``lock on'' to
a _"a changing piece of open source software without an obligatory
specification"_ (Edi Weitz's words in uejo0u...@agharta.de).
However LOOP is in the ANSI spec. Learning to use it to correctly
conform to the spec would make your code most portable and less
suceptible to some (any) "vendor lock in".
| Opinions on what is and isn't "lispy" vary enormously.
I suspect people perceive adding parens makes it somehow makes it more
"lispy". I find the extra syntax that iter adds to be actually less
lispy (iter (for ....)). If truly "lispy" The `for' in functional
position would have `functional meaning'. So iter also boils down to
"arbitrary" syntax implemented in the macro as an exercise in appealing
(pandering!) to the programmer's tastes, and nothing more.
--
Madhu
> Learning to use loop correctly is definately good, ...
That's what I did in this thread.
I first worked with Lispworks and used loop incorrectly. I didn't
notice my fault and was astonished when sbcl complained.
> ... so Jens learn to use loop ...
Thanks for this lecture.
Jens
Don't take lectures from me, I'm far from lisp guru, only opinion. If
you really need one take the intro about loop in PCL, that is the
place where my example comes from, I worked through the chapter with
lw and wondered what Peter is trying to say, than I tried the same
with Allegro and got an error.
Loop for blackbelts. Hm. My belts color is between white and green :)
Jens
> Slobodan Blazeski <slobodan...@gmail.com> writes:
>
> > Learning to use loop correctly is definately good, ...
>
> That's what I did in this thread.
>
> I first worked with Lispworks and used loop incorrectly. I didn't
> notice my fault and was astonished when sbcl complained.
Happens sometimes.
I recently compiled Maxima and LispWorks complained
about some code using LOOP.
The code was (loop ... by #'some-macro ...)
LispWorks rightfully did not allow that. Some other
implementations compiled the code without
problem....
>
> > ... so Jens learn to use loop ...
>
> Thanks for this lecture.
>
> Jens
guile> (define abc '(a b c))
guile> (zip (iota (length abc)) abc)
((0 a) (1 b) (2 c))
guile> (let recur ((i 0) (abc abc))
(if (null? abc) '()
(cons (cons i (car abc)) (recur (1+ i)(cdr abc)))))
((0 . a) (1 . b) (2 . c))
Common Lisp:
(defun zip (&rest lists)
(apply #'mapcar #'list lists))
(defun iota (n)
(loop for i below n collect i))
> (let ((abc '(a b c)))
(zip (iota (length abc)) abc))
((0 A) (1 B) (2 C))