From: JT
Subject: Trouble populating an array of structures with items from list of data
Date: 
Message-ID: <Nu-dnf2pp_PO77PVnZ2dnUVZ_tadnZ2d@giganews.com>
; Suppose I have the following list with data:
;
(defparameter *db*
'(("01" "Agatha Christie" "Murder on the Orient Express" "po")("02" 
"Oscar Wilde" "Portrait of Dorian Gray" "ro")))

; I then create structures for the items
;
(defstruct (bk (:type list)) cod aut tit gen)

; Now, I create an empty array to receive those structures
;
(defvar vc (map-into (make-array 2) #'make-bk))
;
; CL-USER> vc
; => #((NIL NIL NIL NIL) (NIL NIL NIL NIL))

; But when I try to populate the array with the books, like this:
;
(dolist (bk *db*)
	     (map-into (make-array 2) #'make-bk)
        	     (loop for i from 0 to 1 do ;;
		  (setf (aref vc i) bk)))
;
; I find, strangely, this:
;
;CL-USER> vc
;#(("02" "Oscar Wilde" "Portrait of Dorian Gray" "ro")
;  ("02" "Oscar Wilde" "Portrait of Dorian Gray" "ro"))
;
; Probably I problem related to dolist, I'm guessing.
; Any clues?
; Thanks.
; JT.

From: Barry Margolin
Subject: Re: Trouble populating an array of structures with items from list of data
Date: 
Message-ID: <barmar-30E839.02244017052008@newsgroups.comcast.net>
In article <································@giganews.com>,
 JT <··@nowherenow.com> wrote:

> ; Suppose I have the following list with data:
> ;
> (defparameter *db*
> '(("01" "Agatha Christie" "Murder on the Orient Express" "po")("02" 
> "Oscar Wilde" "Portrait of Dorian Gray" "ro")))
> 
> ; I then create structures for the items
> ;
> (defstruct (bk (:type list)) cod aut tit gen)
> 
> ; Now, I create an empty array to receive those structures
> ;
> (defvar vc (map-into (make-array 2) #'make-bk))
> ;
> ; CL-USER> vc
> ; => #((NIL NIL NIL NIL) (NIL NIL NIL NIL))
> 
> ; But when I try to populate the array with the books, like this:
> ;
> (dolist (bk *db*)
> 	     (map-into (make-array 2) #'make-bk)
>         	     (loop for i from 0 to 1 do ;;
> 		  (setf (aref vc i) bk)))
> ;
> ; I find, strangely, this:
> ;
> ;CL-USER> vc
> ;#(("02" "Oscar Wilde" "Portrait of Dorian Gray" "ro")
> ;  ("02" "Oscar Wilde" "Portrait of Dorian Gray" "ro"))
> ;
> ; Probably I problem related to dolist, I'm guessing.
> ; Any clues?
> ; Thanks.
> ; JT.

You have two NESTED loops.  Your outer loop iterates over the elements 
of *DB*, the inner loop then sets every element of VC to that element.  
So the first time through the outer loop you fill VC with the first 
element of *DB*, the second time through you fill VC with the second 
element.

What you want is a single loop that iterates over *DB* and VC in 
parallel:

(loop for bk in *DB*
      for i from 0 to 1
  do (setf (aref vc i) bk))

You also have a third loop using MAP-INTO, that doesn't do anything 
useful.  It fills in an array that's never saved anywhere.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Peter Hildebrandt
Subject: Re: Trouble populating an array of structures with items from list of data
Date: 
Message-ID: <482edf0f$0$90262$14726298@news.sunsite.dk>
Barry Margolin wrote:
> What you want is a single loop that iterates over *DB* and VC in 
> parallel:
> 
> (loop for bk in *DB*
>       for i from 0 to 1
>   do (setf (aref vc i) bk))

Just a small detail, but I'd make it

(loop for bk in *DB*
    for i from 0
    do (setf (aref vc i) bk))

:-)  Peter

> 
> You also have a third loop using MAP-INTO, that doesn't do anything 
> useful.  It fills in an array that's never saved anywhere.
> 
From: Ken Tilton
Subject: Re: Trouble populating an array of structures with items from list of data
Date: 
Message-ID: <482efb0c$0$11643$607ed4bc@cv.net>
Peter Hildebrandt wrote:
> Barry Margolin wrote:
> 
>> What you want is a single loop that iterates over *DB* and VC in 
>> parallel:
>>
>> (loop for bk in *DB*
>>       for i from 0 to 1
>>   do (setf (aref vc i) bk))
> 
> 
> Just a small detail, but I'd make it
> 
> (loop for bk in *DB*
>    for i from 0
>    do (setf (aref vc i) bk))
> 
> :-)  Peter

You guys must be union:

   (apply 'vector *db*)

hth, kenny

-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/
ECLM rant: 
http://video.google.com/videoplay?docid=-1331906677993764413&hl=en
ECLM talk: 
http://video.google.com/videoplay?docid=-9173722505157942928&q=&hl=en
From: Barry Margolin
Subject: Re: Trouble populating an array of structures with items from list of data
Date: 
Message-ID: <barmar-B6A7D8.12295517052008@newsgroups.comcast.net>
In article <·························@cv.net>,
 Ken Tilton <···········@optonline.net> wrote:

> Peter Hildebrandt wrote:
> > Barry Margolin wrote:
> > 
> >> What you want is a single loop that iterates over *DB* and VC in 
> >> parallel:
> >>
> >> (loop for bk in *DB*
> >>       for i from 0 to 1
> >>   do (setf (aref vc i) bk))
> > 
> > 
> > Just a small detail, but I'd make it
> > 
> > (loop for bk in *DB*
> >    for i from 0
> >    do (setf (aref vc i) bk))
> > 
> > :-)  Peter
> 
> You guys must be union:
> 
>    (apply 'vector *db*)

That will certainly work, but it wouldn't have cleared up the OP's 
misunderstanding about why his original code didn't.  And it also won't 
work if *db* is longer than CALL-ARGUMENTS-LIMIT.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Peter Hildebrandt
Subject: Re: Trouble populating an array of structures with items from list of data
Date: 
Message-ID: <482f3ace$0$90264$14726298@news.sunsite.dk>
Barry Margolin wrote:
> In article <·························@cv.net>,
>  Ken Tilton <···········@optonline.net> wrote:
> 
>> Peter Hildebrandt wrote:
>>> Barry Margolin wrote:
>>>
>>>> What you want is a single loop that iterates over *DB* and VC in 
>>>> parallel:
>>>>
>>>> (loop for bk in *DB*
>>>>       for i from 0 to 1
>>>>   do (setf (aref vc i) bk))
>>>
>>> Just a small detail, but I'd make it
>>>
>>> (loop for bk in *DB*
>>>    for i from 0
>>>    do (setf (aref vc i) bk))
>>>
>>> :-)  Peter
>> You guys must be union:
>>
>>    (apply 'vector *db*)
> 
> That will certainly work, but it wouldn't have cleared up the OP's 
> misunderstanding about why his original code didn't.  And it also won't 
> work if *db* is longer than CALL-ARGUMENTS-LIMIT.

Right.  As soon as he has some 500 million entries [1] in there, it's 
getting really dangerous ...

As far as the code snippet is concerned, I must admit that I did not 
even look what it actually did -- "from 0 to 1" just felt wrong ;)

Peter


[1] SBCL 1.0.11.debian:

CL-USER> CALL-ARGUMENTS-LIMIT
536870911
From: Barry Margolin
Subject: Re: Trouble populating an array of structures with items from list of data
Date: 
Message-ID: <barmar-C60B68.20104517052008@newsgroups.comcast.net>
In article <·························@news.sunsite.dk>,
 Peter Hildebrandt <·················@gmail.com> wrote:

> Barry Margolin wrote:
> > In article <·························@cv.net>,
> >  Ken Tilton <···········@optonline.net> wrote:
> > 
> >> Peter Hildebrandt wrote:
> >>> Barry Margolin wrote:
> >>>
> >>>> What you want is a single loop that iterates over *DB* and VC in 
> >>>> parallel:
> >>>>
> >>>> (loop for bk in *DB*
> >>>>       for i from 0 to 1
> >>>>   do (setf (aref vc i) bk))
> >>>
> >>> Just a small detail, but I'd make it
> >>>
> >>> (loop for bk in *DB*
> >>>    for i from 0
> >>>    do (setf (aref vc i) bk))
> >>>
> >>> :-)  Peter
> >> You guys must be union:
> >>
> >>    (apply 'vector *db*)
> > 
> > That will certainly work, but it wouldn't have cleared up the OP's 
> > misunderstanding about why his original code didn't.  And it also won't 
> > work if *db* is longer than CALL-ARGUMENTS-LIMIT.
> 
> Right.  As soon as he has some 500 million entries [1] in there, it's 
> getting really dangerous ...
> 
> As far as the code snippet is concerned, I must admit that I did not 
> even look what it actually did -- "from 0 to 1" just felt wrong ;)

I simply copied that from his original code.  I made the minimum number 
of changes to fix his mistake, so that the transformation would be most 
obvious.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Ken Tilton
Subject: Re: Trouble populating an array of structures with items from list of data
Date: 
Message-ID: <482f8dad$0$15203$607ed4bc@cv.net>
Barry Margolin wrote:
> In article <·························@news.sunsite.dk>,
>  Peter Hildebrandt <·················@gmail.com> wrote:
> 
> 
>>Barry Margolin wrote:
>>
>>>In article <·························@cv.net>,
>>> Ken Tilton <···········@optonline.net> wrote:
>>>
>>>
>>>>Peter Hildebrandt wrote:
>>>>
>>>>>Barry Margolin wrote:
>>>>>
>>>>>
>>>>>>What you want is a single loop that iterates over *DB* and VC in 
>>>>>>parallel:
>>>>>>
>>>>>>(loop for bk in *DB*
>>>>>>      for i from 0 to 1
>>>>>>  do (setf (aref vc i) bk))
>>>>>
>>>>>Just a small detail, but I'd make it
>>>>>
>>>>>(loop for bk in *DB*
>>>>>   for i from 0
>>>>>   do (setf (aref vc i) bk))
>>>>>
>>>>>:-)  Peter
>>>>
>>>>You guys must be union:
>>>>
>>>>   (apply 'vector *db*)
>>>
>>>That will certainly work, but it wouldn't have cleared up the OP's 
>>>misunderstanding about why his original code didn't.  And it also won't 
>>>work if *db* is longer than CALL-ARGUMENTS-LIMIT.
>>
>>Right.  As soon as he has some 500 million entries [1] in there, it's 
>>getting really dangerous ...
>>
>>As far as the code snippet is concerned, I must admit that I did not 
>>even look what it actually did -- "from 0 to 1" just felt wrong ;)
> 
> 
> I simply copied that from his original code.  I made the minimum number 
> of changes to fix his mistake, so that the transformation would be most 
> obvious.
> 

So... no more union jokes? :)

kenny

-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/
ECLM rant: 
http://video.google.com/videoplay?docid=-1331906677993764413&hl=en
ECLM talk: 
http://video.google.com/videoplay?docid=-9173722505157942928&q=&hl=en
From: Thomas A. Russ
Subject: Re: Trouble populating an array of structures with items from list of data
Date: 
Message-ID: <ymizlqmb0gi.fsf@blackcat.isi.edu>
Peter Hildebrandt <·················@gmail.com> writes:

> Barry Margolin wrote:
> > In article <·························@cv.net>,
> >  Ken Tilton <···········@optonline.net> wrote:
> >>    (apply 'vector *db*)
> > That will certainly work, but it wouldn't have cleared up the OP's
> > misunderstanding about why his original code didn't.  And it also
> > won't work if *db* is longer than CALL-ARGUMENTS-LIMIT.
> 
> Right.  As soon as he has some 500 million entries [1] in there, it's
> getting really dangerous ...
> 
> [1] SBCL 1.0.11.debian:
> 
> CL-USER> CALL-ARGUMENTS-LIMIT
> 536870911

Of course, other lisps are not so generous.  LispWorks 5 on Mac/Intel
has a CALL-ARGUMENTS-LIMIT of only 2048.  Clozure Common Lisp doubles
that to 4096.  So does CLISP.  ACL 6.2 and 7.0 (Linux) has 16384.

And the only guarantee you get from the SPEC is that is not be smaller
than 50.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Peter Hildebrandt
Subject: Re: Trouble populating an array of structures with items from list of data
Date: 
Message-ID: <4831f819$0$90267$14726298@news.sunsite.dk>
Thomas A. Russ wrote:
> Peter Hildebrandt <·················@gmail.com> writes:
> 
>> Barry Margolin wrote:
>>> In article <·························@cv.net>,
>>>  Ken Tilton <···········@optonline.net> wrote:
>>>>    (apply 'vector *db*)
>>> That will certainly work, but it wouldn't have cleared up the OP's
>>> misunderstanding about why his original code didn't.  And it also
>>> won't work if *db* is longer than CALL-ARGUMENTS-LIMIT.
>> Right.  As soon as he has some 500 million entries [1] in there, it's
>> getting really dangerous ...
>>
>> [1] SBCL 1.0.11.debian:
>>
>> CL-USER> CALL-ARGUMENTS-LIMIT
>> 536870911
> 
> Of course, other lisps are not so generous.  LispWorks 5 on Mac/Intel
> has a CALL-ARGUMENTS-LIMIT of only 2048.  Clozure Common Lisp doubles
> that to 4096.  So does CLISP.  ACL 6.2 and 7.0 (Linux) has 16384.

Thanks for pointing that out.  I was aware of the 50 arguments 
guaranteed in the spec.  However, I have relied on SBCL's generosity 
before, and I assumed that the major implementations would allow roughly 
the same.

So I better keep this in mind when writing supposedly portable code.

Peter

> And the only guarantee you get from the SPEC is that is not be smaller
> than 50.
> 
From: Thomas A. Russ
Subject: Re: Trouble populating an array of structures with items from list of data
Date: 
Message-ID: <ymive19c0mu.fsf@blackcat.isi.edu>
Peter Hildebrandt <·················@gmail.com> writes:

> Thomas A. Russ wrote:
> > Peter Hildebrandt <·················@gmail.com> writes:
> >> CL-USER> CALL-ARGUMENTS-LIMIT
> >> 536870911
> > Of course, other lisps are not so generous.  LispWorks 5 on Mac/Intel
> > has a CALL-ARGUMENTS-LIMIT of only 2048.  Clozure Common Lisp doubles
> > that to 4096.  So does CLISP.  ACL 6.2 and 7.0 (Linux) has 16384.
> 
> Thanks for pointing that out.  I was aware of the 50 arguments
> guaranteed in the spec.  However, I have relied on SBCL's generosity
> before, and I assumed that the major implementations would allow roughly
> the same.

I suppose that may have something to do with things like assmuptions
about reasonable stack sizes and memory allocation to stacks.  In
general, an application would need to anticipate that the call arguments
limit would need to have (potential) stack memory capacity to handle
this for any arbitary stack frame.

If the implementation normally doesn't have large amounts of memory
allocated to stacks, and there is the possibility that the stack can
collide with other memory structures, you would end up with smaller
limits.


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: ···@informatimago.com
Subject: Re: Trouble populating an array of structures with items from list of 	data
Date: 
Message-ID: <e608a59a-ac5a-47d2-bc88-0c5896faa771@k13g2000hse.googlegroups.com>
On May 17, 5:34 pm, Ken Tilton <···········@optonline.net> wrote:

> You guys must be union:
>
>    (apply 'vector *db*)

Or, done correctly: (make-array (length *db*) :initial-contents *db*)

--
__Pascal Bourguignon__
From: Ken Tilton
Subject: Re: Trouble populating an array of structures with items from list of  data
Date: 
Message-ID: <48306c95$0$11639$607ed4bc@cv.net>
···@informatimago.com wrote:
> On May 17, 5:34 pm, Ken Tilton <···········@optonline.net> wrote:
> 
> 
>>You guys must be union:
>>
>>   (apply 'vector *db*)
> 
> 
> Or, done correctly: (make-array (length *db*) :initial-contents *db*)

Watch out for the shop steward.

kt

-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/
ECLM rant: 
http://video.google.com/videoplay?docid=-1331906677993764413&hl=en
ECLM talk: 
http://video.google.com/videoplay?docid=-9173722505157942928&q=&hl=en