From: Peter Whincop
Subject: newbie array question
Date: 
Message-ID: <6esoub$tim$1@news.fas.harvard.edu>
Hi everyone,
This is possibly a brain-dead question: I know I can make an array:
	(setf arr (make-array 3 [etc]))
and:
	(setf dim 3)
	(setf arr (make-array dim))
but can't do this:
	(setf arr (make-array '(3 dim)))
because "dim" causes the interpreter some grief. I would like to know: how
is it possible to make such a 2D array, whose dimensions are given as
variables, as in my failed version above? Slade has a solution in his OOCL
book, but I was wondering if the facility was already lurking somewhere in
the language. I use ACL4.3 on Linux.

Thank you,

Peter
(music type of person)

From: Christopher J. Vogt
Subject: Re: newbie array question
Date: 
Message-ID: <351209A6.48929D85@computer.org>
Peter Whincop wrote:
> 
> Hi everyone,
> This is possibly a brain-dead question: I know I can make an array:
>         (setf arr (make-array 3 [etc]))
> and:
>         (setf dim 3)
>         (setf arr (make-array dim))
> but can't do this:
>         (setf arr (make-array '(3 dim)))
> because "dim" causes the interpreter some grief. I would like to know: how
> is it possible to make such a 2D array, whose dimensions are given as
> variables, as in my failed version above? Slade has a solution in his OOCL
> book, but I was wondering if the facility was already lurking somewhere in
> the language. I use ACL4.3 on Linux.

I think that this is a common newbie question.

(setf arr (make-array (list 3 dim)))

-- 
Christopher J. Vogt - Computer Consultant - Lisp, AI, Graphics, etc.
http://members.home.com/vogt/
From: Peter Whincop
Subject: Re: newbie array question
Date: 
Message-ID: <6et2oh$fst$1@news.fas.harvard.edu>
The answer is (as given by generous readers):

(setf arr (make-array (list 3 dim)))

Thanks to all.

Peter
From: Stig Hemmer
Subject: Re: newbie array question
Date: 
Message-ID: <ekvn2elptda.fsf@ra.pvv.ntnu.no>
·······@fas.harvard.edu (Peter Whincop) writes:
> 	(setf arr (make-array '(3 dim)))

Just to provide an alternative solution:
        (setf arr (make-array `(3 ,dim)))

In an example as simple as this, the solution using (list) is good.
However, when things get the tiniest bit more complicated the nested
(list)s can become pretty unreadable.  Then backquote is your friend.

E.g. consider:

(setf dummy '(foo (bar (baz 3))))

Now you want a variable instead of the 3:

(setf dummy `(foo (bar (baz ,variable))))
(setf dummy (list 'foo (list 'bar (list 'baz variable))))

I know which one I prefer...

Stig Hemmer.
From: Espen Vestre
Subject: Re: newbie array question
Date: 
Message-ID: <w6k99pxz73.fsf@gromit.nextel.no>
Stig Hemmer <····@pvv.ntnu.no> writes:

> In an example as simple as this, the solution using (list) is good.
> However, when things get the tiniest bit more complicated the nested
> (list)s can become pretty unreadable.  Then backquote is your friend.

...but remember to be very careful if you use backquote in constructor-
functions for objects that will be used in destructive code, because
lisp is allowed to treat parts of the backquoted list as a constant.

Here's a very simple example of the dangers of backquote (the same
holds for ordinary quote as well, but there it's a bit more obvious!).

(defun bqput (x)
  `(,x in a list))

(defun listput (x)
  (list x 'in 'a 'list))

USER(1661): (setf x (bqput 'gazonk))
(GAZONK IN A LIST)
USER(1662): (setf y (bqput 'bar))
(BAR IN A LIST)
USER(1663): (setf (third y) 'this)
THIS
USER(1664): y
(BAR IN THIS LIST)
USER(1665): x
(GAZONK IN THIS LIST)

USER(1666): (setf x (listput 'bar))
(GAZONK IN A LIST)
USER(1667): (setf y (listput 'bar))
(BAR IN A LIST)
USER(1670): (setf (third y) 'this)
THIS
USER(1671): y
(BAR IN THIS LIST)
USER(1672): x
(GAZONK IN A LIST)

(and btw, if you try to call bqput _again_, you'll se that the very
same list that's used by bqput in subsequent calls, was modified:)

USER(1673): (bqput 'newthing)
(NEWTHING IN THIS LIST)


> (setf dummy `(foo (bar (baz ,variable))))
> (setf dummy (list 'foo (list 'bar (list 'baz variable))))
>
> I know which one I prefer...

Me too. As long as you keep in mind that the two solutions aren't 
_equivalent_, the first one is much better.

--

regards,
  Espen Vestre
From: Espen Vestre
Subject: Re: newbie array question
Date: 
Message-ID: <w6iup9xy2u.fsf@gromit.nextel.no>
Espen Vestre <··@nextel.no> writes:

> USER(1666): (setf x (listput 'bar))
> (GAZONK IN A LIST)

just in case you wondered, this should have been

> USER(1666): (setf x (listput 'gazonk))
> (GAZONK IN A LIST)

(I run my lisp under emacs and sometimes have the habit of editing
 previous lines...)

--

  Espen