Next: , Previous: , Up: k9: Manual  


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 List of Lists Indexing

A list of lists can be indexed by succesive index operations left to right.

 x:3^!12;x  / cut a list into a matrix or list of lists
0 1 2 3  
4 5 6 7  
8 9 10 11

 x[0]        / index the first element which is a list
0 1 2 3

 x[0 2]      / index two elements, the 0 and 2 elements
0 1 2 3  
8 9 10 11

 x[0;2]      / index the first row and then 2 element of that row
2

 x[;2][0]    / index the 2 column and then the 0 row
2

 x[0][2]     / index the 0 row and then the 2 column
2

5.4 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.5 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.6 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.6.1 Pairwise

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

  • x+y : Add
  • 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: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.6.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^y : Reshape all element in y by x
  • x#y : List all elements in x that appear in y
  • x?y : Indices of elements of y in x else return the length of 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.6.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_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: Dictionary, Previous: Noun, Up: k9: Manual