From: Andreas Yankopolus
Subject: Initializing lists/arrays with structures
Date: 
Message-ID: <20040227113107430-0500@atl.news.speakeasy.net>
Is there a standard way to initialize a list or array with multiple 
instances of a structure? In the following naive example, each element 
of the list points to the same instance of element:

[72]> (defstruct element (x 0) (y 0))
ELEMENT
[73]> (setf foo (make-list 3 :initial-element (make-element)))
(#S(ELEMENT :X 0 :Y 0) #S(ELEMENT :X 0 :Y 0) #S(ELEMENT :X 0 :Y 0))
[74]> (setf (element-x (car foo)) 1)
1
[75]> foo
(#S(ELEMENT :X 1 :Y 0) #S(ELEMENT :X 1 :Y 0) #S(ELEMENT :X 1 :Y 0))

I've run into similar problems with make-array, since :initial-contents 
want a list.

Thanks,

Andreas

From: Peter Seibel
Subject: Re: Initializing lists/arrays with structures
Date: 
Message-ID: <m3wu6830rg.fsf@javamonkey.com>
Andreas Yankopolus <·······@nospam.yank.to> writes:

> Is there a standard way to initialize a list or array with multiple 
> instances of a structure? In the following naive example, each element 
> of the list points to the same instance of element:
> 
> [72]> (defstruct element (x 0) (y 0))
> ELEMENT
> [73]> (setf foo (make-list 3 :initial-element (make-element)))
> (#S(ELEMENT :X 0 :Y 0) #S(ELEMENT :X 0 :Y 0) #S(ELEMENT :X 0 :Y 0))
> [74]> (setf (element-x (car foo)) 1)
> 1
> [75]> foo
> (#S(ELEMENT :X 1 :Y 0) #S(ELEMENT :X 1 :Y 0) #S(ELEMENT :X 1 :Y 0))


  (loop repeat 3 collect (make-element))

> I've run into similar problems with make-array, since :initial-contents 
> want a list.

  (make-array 3 :initial-contents (loop repeat 3 collect (make-element)))

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: Andreas Yankopolus
Subject: Re: Initializing lists/arrays with structures
Date: 
Message-ID: <20040228202114248-0500@atl.news.speakeasy.net>
Peter,

Thanks for your succinct solution to my list/array initialization 
problem. Your use of collect was really slick; I wasn't familiar with 
that function.

-Andreas
From: Tim Bradshaw
Subject: Re: Initializing lists/arrays with structures
Date: 
Message-ID: <ey37jy8fib0.fsf@cley.com>
* Andreas Yankopolus wrote:
> Is there a standard way to initialize a list or array with multiple 
> instances of a structure? In the following naive example, each element 
> of the list points to the same instance of element:

Something like this will do but is far from optimal

(defmacro make-array-initializing (dim/s &body forms)
  `(loop with .f. = #'(lambda () ,@forms)
         and .a. = (make-array ,dim/s)
         for .i. from 0 below (array-total-size .a.)
         do (setf (row-major-aref .a. .i.) (funcall .f.))
         finally (return .a.)))

(make-array-initializing '(1 2 3) (random 10))

--tim
From: Wolfhard Buß
Subject: Re: Initializing lists/arrays with structures
Date: 
Message-ID: <m3hdxc2t59.fsf@buss-14250.user.cis.dfn.de>
* Andreas Yankopolus writes:

> Is there a standard way to initialize a list or array with multiple 
> instances of a structure?

How about map-into?

 (map-into <sequence> <function> . <sequences>)

Example:

 (map-into (make-sequence <type> <length>) <constructor>)


-- 
"Hurry if you still want to see something. Everything is vanishing."
                                       --  Paul C�zanne (1839-1906)