Hello. Some questions about LISP arrays:
1) How do I create 2D arrays on the fly? Wille [[1 2 3] [4 5 6] [7 8 9]]
work?
2) What do I use for referencing in an array and what is the first element's
index (0 or 1)?
Wouter Lievens wrote:
> Hello. Some questions about LISP arrays:
>
> 1) How do I create 2D arrays on the fly? Wille [[1 2 3] [4 5 6] [7 8 9]]
> work?
> 2) What do I use for referencing in an array and what is the first element's
> index (0 or 1)?
>
>
>
If you want to use a 2D-array:
#2a((1 2 3) (4 5 6) (7 8 9))
The #2a means that it's a 2-dimensional array.
Also, check out make-array, array-dimension and array-dimensions:
> (setf my-array (make-array '(3 4) :initial-element 0))
MY-ARRAY
> my-array
#2a((0 0 0 0) (0 0 0 0) (0 0 0 0))
> (array-dimension my-array 0)
3
> (array-dimension my-array 1)
4
> (array-dimensions my-array)
(3 4)
Elements are accessed via aref, in row-order:
> (setf my-array #2a((1 2 3) (4 5 6) (7 8 9)))
MY-ARRAY
> (aref my-array 0 0)
1
> (aref my-array 2 0)
7
> (setf (aref my-array 2 2) 8)
8
> my-array
#2a((1 2 3) (4 5 6) (7 8 8))
Hope that's what you were looking for,
Justin Dubs
Wouter Lievens wrote:
> Hello. Some questions about LISP arrays:
See chapter 15 in the HyperSpec at
http://www.lispworks.com/reference/HyperSpec/, or chapter 17 in CLtL2 at
http://www-2.cs.cmu.edu/afs/cs.cmu.edu/project/ai-repository/ai/html/cltl/cltl2.html
> 1) How do I create 2D arrays on the fly? Wille [[1 2 3] [4 5 6] [7 8 9]]
> work?
The function make-array has an option to set the initial contents of a
newly created array. That's what you want to look for.
> 2) What do I use for referencing in an array and what is the first element's
> index (0 or 1)?
Array access is done via (aref an-array index). Arrays in CL are zero-bound.
note:
read access <=> (aref an-array index)
write access <=> (setf (aref an-array index) value)
Pascal
--
Pascal Costanza University of Bonn
···············@web.de Institute of Computer Science III
http://www.pascalcostanza.de R�merstr. 164, D-53117 Bonn (Germany)
"Wouter Lievens" <·········@telefragged.com> writes:
> Hello. Some questions about LISP arrays:
>
> 1) How do I create 2D arrays on the fly?
make-array
http://www.lispworks.com/reference/HyperSpec/Body/f_mk_ar.htm
If you are not going to be modifiyng the array, you could try:
#2A((1 2 3) (4 5 6) (7 8 9))
> 2) What do I use for referencing in an array
aref
http://www.lispworks.com/reference/HyperSpec/Body/f_aref.htm
> and what is the first element's index (0 or 1)?
I'll leave that one as an exercise :-)
"Wouter Lievens" <·········@telefragged.com> writes:
> Hello. Some questions about LISP arrays:
>
> 1) How do I create 2D arrays on the fly? Wille [[1 2 3] [4 5 6] [7 8 9]]
> work?
No.
(make-array '(2 2) :initial-contents '((1 2 3) (4 5 6) (7 8 9)))
If you want the above notation you can always define your read macro
on `['.
> 2) What do I use for referencing in an array and what is the first element's
> index (0 or 1)?
You use the function AREF
(aref four-dimensional-array 1 0 0 3)
where `four-dimensional-array' is a variable bound to a ... 4D array.
Arrays are 0-indexed.
Cheers
--
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group tel. +1 - 212 - 998 3488
715 Broadway 10th Floor fax +1 - 212 - 995 4122
New York, NY 10003, USA http://bioinformatics.cat.nyu.edu
"Hello New York! We'll do what we can!"
Bill Murray in `Ghostbusters'.