ExpandCollapsePrev Next Index

+ 14.1 Arrays

+ 14.1.1 Array Values

When you write a tuple like this:

    val x = 1,2,3,4,5;

where all the components have the same type, it is called an array value. A special notation may be used for this array type:


that is, int multipled by itself 5 times: this type is exactly the same (not equivalent or isomorphic but exactly the same) as:

    int * int * int * int * int

There is also an alias in the library which can be used:


This kind of array is a first class value, it is not an object.

+ 14.1.2 Array element

You can get an element from an array by its position:

  println$ a.1;
  println$ 1 a;

Note the position, or index, is zero origin in accordance with C conventions. The index can be an expression of any core integral type. The function 1 used here is called an array projection function.

+ 14.1.3 Array length

The length of an array can be found by the len function. The type of the result is size, an integral type used to determine the sizes of things.

  println$ a.len;

+ 14.1.4 Array member

You can test if a value is in an array easily:

  println$ 2 \(\in\) a;

Note the beautiful infix set membership operator is spelled:

  "2 \in a"

This is one of the standard TeX operators that the webserver displays using MathJax.

+ 14.1.5 Array iterators

Perhaps the most interesting thing you can do with an array is iterate through its elements:

  val a = 1,2,3,4;
  for var i in 0 upto 3 do
    println$ "a."+str i+"="+str a.i;

This is an indexed based loop specialised for arrays. Here's another iterator:

  for v in a do
    println$ "element=" + v;

This form of iterator can be used with any sequence. It has the advantage it isn't possible to get an out of bounds index. The disadvantage is that you can't access the index at all.

There's yet another way to do this:

  iter (proc (v:int) {println$ "element=" + str v; }) a;