The purpose of the following code should be reasonably obvious, but I'm
wondering: Is there a way to do it that doesn't involve me using loop for
something that clearly has nothing to do with looping?
(let ((left-rotated '((1 2 3) 4 5)))
(loop for ((a b c) d e) on left-rotated
return (list a b (list c d e))))
=> (1 2 (3 4 5))
Svein Ove Aas wrote:
> The purpose of the following code should be reasonably obvious, but I'm
> wondering: Is there a way to do it that doesn't involve me using loop for
> something that clearly has nothing to do with looping?
>
> (let ((left-rotated '((1 2 3) 4 5)))
> (loop for ((a b c) d e) on left-rotated
> return (list a b (list c d e))))
(let ((left-rotated '((1 2 3) 4 5)))
(destructuring-bind ((a b c) d e)
left-rotated
(list a b (list c d e))))
Jeremy.
Jeremy Yallop wrote:
> Svein Ove Aas wrote:
>> The purpose of the following code should be reasonably obvious, but I'm
>> wondering: Is there a way to do it that doesn't involve me using loop
>> for something that clearly has nothing to do with looping?
>>
>> (let ((left-rotated '((1 2 3) 4 5)))
>> (loop for ((a b c) d e) on left-rotated
>> return (list a b (list c d e))))
>
> (let ((left-rotated '((1 2 3) 4 5)))
> (destructuring-bind ((a b c) d e)
> left-rotated
> (list a b (list c d e))))
>
Ooh, thanks.
1 symbol down, only 950 to go...
Jeremy Yallop <······@jdyallop.freeserve.co.uk> writes:
> Svein Ove Aas wrote:
>> (loop for ((a b c) d e) on left-rotated
> (destructuring-bind ((a b c) d e)
There are some differences between these types of destructuring.
DESTRUCTURING-BIND should signal an error if a data list is too
short. LOOP binds variables to NIL instead.
DESTRUCTURING-BIND recognizes lambda list keywords. To LOOP,
they are just symbols. I once wanted to
(loop for (english-whole english-root foreign-parts foreign-whole
&key lang ref obs arc) in *word-list*
collect (make-instance 'word
:english-whole english-whole
:english-root english-root
:foreign-parts foreign-parts
:foreign-whole foreign-whole
:foreign-language lang
:references (designated-list ref)
:obsoletep obs
:archaicp arc)))
but had to do the destructuring with a separate DESTRUCTURING-BIND.
Svein Ove Aas wrote:
> The purpose of the following code should be reasonably obvious, but I'm
> wondering: Is there a way to do it that doesn't involve me using loop
> for something that clearly has nothing to do with looping?
>
> (let ((left-rotated '((1 2 3) 4 5)))
> (loop for ((a b c) d e) on left-rotated
> return (list a b (list c d e))))
>
> => (1 2 (3 4 5))
I might add the following: Are there any gotchas with using backquote
instead of list, in this case?
Any sharing of structure with `(,a ,b (,c ,d ,e))?
Svein Ove Aas <··············@brage.info> writes:
> I might add the following: Are there any gotchas with using backquote
> instead of list, in this case?
>
> Any sharing of structure with `(,a ,b (,c ,d ,e))?
There aren't any cons cells with both elements constant, so there can't be
any sharing.
--
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist