XLISP aref

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference

# aref

Type: | - | function (subr)
Source: | - | xlbfun.c

## Syntax

(aref array element)
  array - an array expression
  element - an integer expression
  returns - the value of the array element

## Description

The 'aref' function returns the specified element out of a previously created array. Array elements may be any valid lisp data type, including lists or arrays. Arrays made by make-array and accessed by 'aref' are base 0. This means the first element is accessed by element number '0' and the last element is accessed by element number 'n-1' [where 'n' is the array size]. Array elements are initialized to NIL.

## Examples

    (setq my-array '#(0 1 2 3 4))       => #(0 1 2 3 4)
    (aref my-array 0)                   => 0
    (aref my-array 4)                   => 4
    (aref my-array 5)                   => error: array index out of bounds - 5
    my-array                            => #(0 1 2 3 4)

    (setq new (make-array 4))           => #(NIL NIL NIL NIL)
    (setf (aref new 0) (make-array 4))  => #(NIL NIL NIL NIL)
    new                                 => #(#(NIL NIL NIL NIL) NIL NIL NIL)
    (setf (aref (aref new 0) 1) 'a)     => A
    new                                 => #(#(NIL A NIL NIL) NIL NIL NIL)
    (setf (aref new 2) '(a b c))        => (A B C)
    new                                 => #(#(NIL A NIL NIL) NIL (A B C) NIL)

Read macro: There is a built-in read-macro for arrays, '#(...)' [the hash symbol with the array elements in parentheses]. This allows you to create arbitrary arrays with initial values without going through a make-array function. See the Readtable section in the XLISP 2.0 Manual.

Note: This function returns the value of an array element. However, there is no equivalent direct function to set the value of an array element to some value. To set an element value, you must use the setf function. The setf function is a generalized function that allows you to set the value of arbitrary lisp entities.

See also:

- Contents → Array Functions

Back to Top

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference