Next: , Previous: , Up: Top  


5 List

Lists and derivatives of lists are fundamental to k9 which makes sense 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 an atom or a list, one can use 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 list
`I

Commands that generate sequences of numbers return lists regardless of whether the count (length of the list) is 1 or many.

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

5.1 List Syntax

In general, lists consist of elements separated by semicolons and encased by parenthesis.

 (1;3;12)         / list of ints
1 3 12

@(1;3.;`a;"b")   / non-uniform list
`L

@((1;3);(12;0))  / list of integer lists
LI

@'((1;3);(12;0)) / each list is type I
`I`I

,,,,,(3;1)      / a list of a list of a list..
,,,,,3 1

5.2 List Indexing

Lists can be indexed in different ways. The @ notation is often used as it’s fewer characters than [] and the explicit @ instead of space is likely more clear.

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

5.3 Updating List Elements

Lists can be updated elementwise by setting the indexed element to a required value. There is also a syntax for updating many elements and that is found at amend.

 a:2*1+!10;a
2 4 6 8 10 12 14 16 18 20
 a[3]:80
 a
2 4 6 80 10 12 14 16 18 20

5.4 Function Applied to Lists

Most functions can be applied to lists without special syntax as if it was an element.

 x:!3;x
0 1 2

 x+10
10 11 12

 +\x
0 1 3

 +/x
3

 {x*x:sin x}[x]
0 0.7080734 0.8268218

5.5 Functions Applied to 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]) would also be possible.

5.5.1 Pairwise

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

 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

5.5.2 Each Element of One List Compared to Entire Other List

These functions compare x[i] to y or x to y[i]. They are not symmetric to their inputs, i.e. f[x;y] does not equal f[y;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

5.5.3 Each List Used Symmetrically

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

 x:2 8 20
 y:1 2 3 7 8 9
 x_y
1 3 7 9

Next: , Previous: , Up: Top