Next: , Previous: , Up: Top

## 7 List

Lists and derivates of lists are core to k9 which likely is not a surprise given that the language is made to process large quantites of data. Performance will be best when working with uniform lists of a single data type but k9 supports list of non-uniform type also.

Lists are automatically formed when a sequence of uniform type are entered or generated by any function.

``` 1 3 12      / list of ints
1 3 12

3.1 -4.1 5. / list of floats
3.1 -4.1 5.

"abc"       / list of chars
"abc"

`x`y`z      / list of names
`x`y`z
```

In order to determine if data is a atom or list one can you the type command. The command returns a lower case value for atoms and an upper case value for lists.

``` @1          / an integer
i

@1 3 12     / list of ints
I

@,1         / list of single int via enlist
I
```

Commands that generate sequences of numbers return lists regardless if the count is 1 or many.

``` @!0
`I
@!1
`I
@!2
`I
```

### 7.1 List Syntax

In general, lists are created by data separated by semicolons and encased by parenthesis.

``` (1;3;12)         / list of ints
@(1;3.;`a;"b")   / non-uniform list
L
@((1;3);(12;0))  / list of lists
L
@'((1;3);(12;0)) / each list is type I
`I`I
,,,,,(3;1)      / a list of a list of a list ...
,,,,,3 1
```

### 7.2 List Indexing

Lists can be indexed by using a few notations. The @ notation is often used as it’s less characters than [] and the explicit @ instead of space is likley more clear.

``` a:2*1+!10 / 2 4 ... 20
a      / square bracket
20
a@9       / at
20
a 9       / space
20
a(9)      / parenthesis
20
a     / out of range return zero
0
```

### 7.3 Updating List Elements

Lists can be updated element wise but typically one is likely to be updating many elements and there is a syntax for doing so.

``` a:2*1+!10
a
?2 4 6 8 10 12 14 16 18 20
a:80
a
2 4 6 80 10 12 14 16 18 20
a:@[a;0 2 4 6 8;0];a
0 4 0 80 0 12 0 16 0 20
a:@[a;1 3 5;*;100];a
0 400 0 8000 0 1200 0 16 0 20
a:@[a;!#a;:;0];a
0 0 0 0 0 0 0 0 0 0
```

List amend syntax has a few options so will be explained in more detail. In this section list (L), indices (I), value (v), identity fucntion (:), and general function (f) will be represented by a single character. Spaces have been added for readability but are not needed.

• @[L; I; v]
• @[L; I; :; v]
• @[L; I; f; v]

The first syntax sets the list at the indices to value. The second syntax performs the same modificaiton but explicitly lists the identity function, :. The third synatx is the same as the preceeding but uses an arbitrary function.

Often the developer will need to determine which indices to modify and in cases where this isn’t onerous it can be done in the function.

``` a:2*1+!10
@[a;&a<14;:;-3]
-3 -3 -3 -3 -3 -3 14 16 18 20
@[!10;1 3 5;:;10 20 30]
0 10 2 20 4 30 6 7 8 9
@[!10;1 3 5;:;10 20]   / index and value array length mistmatch
:length
@[!10;1 3;:;10 20 30]  / index and value array length mistmatch
:length
```

### 7.4 Function of Two Lists

This section will focus on functions (f) that operate on two lists (x and y). As these are internal functions examples will be shown with infix notation (x+y) but prefix notation (+[x;y]) is also permissible.

#### 7.4.1 Pairwise

These functions operate on list elements pairwise and thus requires that x and y are equal length.

• x-y : Subtract
• x*y : Multiply
• x%y : Divide
• x&y : AND/Min
• x|y : OR/Max
• x>y : Greater Than
• x<y : Less Than
• x=y : Equals
• x!y : Dictionary
• x\$y : Sumproduct
``` x:1+!5;y:10-2*!5
x
1 2 3 4 5
y
10 8 6 4 2
x+y
11 10 9 8 7
x-y
-9 -6 -3 0 3
x*y
10 16 18 16 10
x%y
0.1 0.25 0.5 1 2.5f
x&y
1 2 3 4 2
x|y
10 8 6 4 5
x>y
00001b
x<y
11100b
x=y
00010b
x!y
1|10
2| 8
3| 6
4| 4
5| 2
x\$y
70
```

#### 7.4.2 Each Element of One List Compared to Entire Other List

These functions compare x[i] to y or x to y[i] and f is not symmetric to its inputs, i.e. f[x;y] does not equal f[y;x];

• x^y : Reshape all element in y by x
• x#y : List all elements in x that appear in y
• x?y : Find all elements in y from x
``` x:0 2 5 10
y:!20
x^y
0 1
2 3 4
5 6 7 8 9
10 11 12 13 14 15 16 17 18 19

x:2 8 20
y:1 2 3 7 8 9
x#y
2 8
x?y
3 0 3 3 1 3
```

#### 7.4.3 Each List Used Symmetrically

These functions are symmetric in the inputs f[x;y]=f[y;x] and the lists are not required to be equal length.

• x_y : Values present in only one of the two lists
``` x:2 8 20
y:1 2 3 7 8 9
x_y
1 3 7 9
```

Next: , Previous: , Up: Top