Next: , Previous: , Up: Top  


2 Verbs

This chapter covers verbs which are the core primitives of k9. Given how different is it to call functions in k9 than in most other languages, you may have to read this chapter a few times. Once you can “speak” k9 you’ll read |x better than reverse(x).

Most functions are overloaded and change depending on the number of arguments. This can confuse new users. Eg. (1 4)++(2 3;5 6;7 8) contains the plus symbol once as flip and then for addition. (Remember evaluation is right to left!)

There are verbs that can take a function argument. Those are listed with a ’[f]’ prefix. These include [f]cut, [f]take, and [f]drop. A few can take a special name prefix and those are listed with a ’n’ prefix and include [n]find, and [n]at.

Verb
: x,         set.
+ flip,      plus.
- negate,    minus.
* first,     times.
%            divide.
& where,     min/and.
| reverse,   max/or.
< asc,       less.
> desc,      more.
= group,     equal.
~ not,       match.
! enum,      key.
, list,      cat.
^ sort,   [f]cut. 
# count,  [f]take.  
_ floor,  [f]drop.    
$ string,    parse.    
? unique, [n]find.    
@ type,   [n]at.    
. value,     dot.

2.1 x ⇒ :x

2.2 set ⇒ x:y

Set a variable, x, to a value, y. There should be no space between x and :.

 a:3
 a
3
 b:(`green;37;"blue")
 b
green
37
blue
 c:{x+y}
 c
{x+y}
 c[12;15]
27

2.3 flip ⇒ +x

Flip, or transpose, x.

 x:((1 2);(3 4);(5 6))
 x
1 2
3 4
5 6
 +x
1 3 5
2 4 6
 `a`b!+x
a|1 3 5
b|2 4 5
 +`a`b!+x
a b
- -
1 2
3 4
5 6

2.4 plus ⇒ x+y

Add x and y. Arguments can be elements or lists but if both x and y are lists then they must be of equal length.

 3+7
10

a:3;
 a+8
11

 3+4 5 6 7
7 8 9 10

3 4 5+4 5 6
7 9 11

 3 4+1 2 3        / lengths don't match
:length

 10:00+1         / add a minute
10:01

 10:00:00+1      / add a second
10:00:01

 10:00:00.000+1  / add a millisecond
10:00:00.001

2.5 negate ⇒ -x.

 -3
-3
 --3
3
 x:4;
 -x
-4
 d:`a`b!((1 2 3);(4 5 6))
 -d
a|-1 -2 -3
b|-4 -5 -6

2.6 minus ⇒ x-y.

Subtract y from x.

 5-2
3
 x:4;y:1;
 x-y
3

2.7 first ⇒ *x

Return the first value of x.

 *1 2 3
1
 *((1 2);(3 4);(5 6))
1 2
 **((1 2);(3 4);(5 6))
1
 *`a`b!((1 2 3);(4 5 6))
1 2 3
 *|1 2 3
3

2.8 times ⇒ x*y

Mutliply x and y.

 3*4
12
 3*4 5 6
12 15 18
 1 2 3*4 5 6
4 10 18

2.9 divide ⇒ x%y

Divide x by y.

 12%5
2.4
 6%2    / division of two integers returns a float
3f

2.10 where ⇒ &x

Given a list of boolean values return the indices where the value is non-zero.

 &001001b
2 5
 "banana"="a"
010101b
 &"banana"="a"
1 3 5
 x@&30<x:12.7 0.1 35.6 -12.1 101.101  / return values greater than 30
35.6 101.101

Given a list of non-negative integer values, eg. x[0], x[1], ..., x[n-1], generate x[0] values of 0, x[1] values of 1, ..., and x[n-1] values of n-1.

 & 3 1 0 2
0 0 0 1 3 3

2.11 and ⇒ x&y

The smaller of x and y. One can use the over adverb to determine the min value in a list.

 3&2
2
 1 2 3&4 5 6
1 2 3
 010010b&111000b
010000
 `a&`b
`a
 &/ 3 2 10 -200 47
-200

2.12 reverse ⇒ |x

Reverse the list x.

 |0 3 1 2
2 1 3 0
 |"banana"
"ananab"
 |((1 2 3);4;(5 6))
5 6  
4    
1 2 3

To get the last element, one can take the first element of the reverse list (*|`a`b`c).

2.13 or ⇒ x|y

The greater of x and y. Max of a list can be determine by use of the adverb over.

 3|2
3
 1 2 3|4 5 6
4 5 6
 101101b|000111b
101111b
 |/12 2 3 10 / use over to determine the max of a list
12

2.14 asc(desc) ⇒ < (>) x

Sort a list or dictionary in ascending (<) or descending (>) order. Applied to a list, these will return the indices to be used to achieve the sort.

 <5 7 0 12
2 0 1 3

 x@<x:5 7 0 12
0 5 7 12

 d:`a`b`c!3 2 1;d
a|3
b|2
c|1

 <d / a dictionary is sorted by value
c|1
b|2
a|3

2.15 less (more) ⇒ x < (>) y

Return true (1b) if x is less (more) than y else false (0b).

 3<2
0b
 2<3
1b
 1 2 3<4 5 6
111b
 ((1 2 3);4;(5 6))<((101 0 5);12;(10 0)) / size needs to match
101
1  
10
 "a"<"b"
1b

2.16 group ⇒ =x

A dictionary of the distinct values of x (key) and their indices (values).

 ="banana"
a|1 3 5
b|0    
n|2 4  
 =0 1 0 2 10 7 0 1 12
 0|0 2 6
 1|1 7  
 2|3    
 7|5    
10|4    
12|8    

2.17 equal ⇒ x=y

Return true (1b) if x is equal to y else false (0b).

 2=2
1b
 2=3
0b
 2=2.
1b
 "banana"="abnaoo" / check strings of equal length by character
001100b
 "banana"="apple"  / unequal length strings error
^
:length

2.18 not ⇒ ~x

Boolean invert of x

 ~1b
0b
 ~101b
010b
 ~37 0 12
010b

2.19 match ⇒ x~y

Return true (1b) if x matches y else false (0b). A match happens if the two arguments evaluate to the same expression.

 2~2
1b
 2~3
0b
 2~2.
0b
 "banana"~"apple"
0b
 `a`b~`a`b  / different than = which is element-wise comparison
1b
 `a`b=`a`b
11b
 f:{x+y}
 f~{x+y}
1b

2.20 enum ⇒ !x

Enumerate the domain of x. Given an integer, x, generate an integer list from 0 to x-1. Given a dictionary, x, return all key values.

 !3
0 1 2

 !`a`b`c!3 2 1
`a`b`c

In the case where x is a list of integers x1 x2 ... xk, generate a matrix of k rows where each row has size */x1 x2 ... xk and the elements of row i are !xi repeated as many times as necessary. Each row’s repeats must fit below constant values of the row above.

 / Each row of length 2*3*2 = */2 3 2 = 12.
 / First row values 0,1
 / Second row values 0,1,2
 / Third row values 0,1
 !2 3 2 
0 0 0 0 0 0 1 1 1 1 1 1
0 0 1 1 2 2 0 0 1 1 2 2
0 1 0 1 0 1 0 1 0 1 0 1

 !4 2 3
0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3
0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1
0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2

 !2 2 2
0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1

 +!2 2 2
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

2.21 key ⇒ x!y

Dictionary of x (key) and y (value).

 3!7
,3!,7
 `a`b!3 7
a|3
b|7
 `a`b!((1 2);(3 4))
a|1 2
b|3 4

2.22 enlist ⇒ ,x

Create a list from x

 ,3
,3
 ,1 2 3
1 2 3
 3=,3
,1b
 3~,3
0b

2.23 cat ⇒ x,y

Concatenate x and y.

 3,7
3 7

 "hello"," ","there"
"hello there"

 C:("ab";"c";("def";"ghi"));C
ab           
c            
("def";"ghi")

 ,/C     / join the list once
a  
b  
c  
def
ghi

 ,//:C   / converge over join until single list
"abcdefghi"

2.24 sort ⇒ ^x

Sort list, dictionary, or table x into ascending order. Dictionaries are sorted using the keys and tables by the first column field. One can sort tables by arbitrary columns by first reordering the columns in the table using take or by extracting the sort column by index or expression.

 ^0 3 2 1
0 1 2 3

^`b`a!((0 1 2);(7 6 5))     / sort dictionary by key
[a:7 6 5;b:0 1 2]

 ^[[]z:`c`a`b;y:3 2 1]      / sort table by 1st col
z y
- -
a 2
b 1
c 3

 ^`y`z#[[]z:`c`a`b;y:3 2 1] / sort table by new 1st col
y z
- -
1 b
2 a
3 c

2.25 [f]cut ⇒ x^y

Cut list y by size, indices, or function x.

actionx
rowspositive int
columnsnegative int
indiceslist
functionfunction

Positve integer x, cut y into x rows

 3^101+!20   / 3 rows
101 102 103 104 105 106
107 108 109 110 111 112
113 114 115 116 117 118

Negative integer, cut y into x elements per row

 -3^101+!20  / 3 elements in each row
101 102 103
104 105 106
107 108 109
110 111 112
113 114 115
116 117 118

Positive integer array x, cut at each element in x

 2 3 7 ^!17  / left list indicates start number of each row.
,2                        
3 4 5 6                   
7 8 9 10 11 12 13 14 15 16

 8 9 10 11 12^.1*!20
,0.8                           
,0.9                           
,1.                            
,1.1                           
1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9

Function returning boolean array, cut at each index where f is non-zero.

 {(x*x)within .5 1.5}^.1*!20
0.8                            
0.9                            
1.                             
1.1                            
1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9

2.26 count ⇒ #x

Count the number of elements in list, dictionary, or table x.

 #0 1 2 12
4

 #((0 1 2);3;(4 5))
3
 #`a`b!((1 2 3);(4 5 6)) / count the number of keys
2

 #5                      / will error as an element
#5
^
!class

2.27 [f]take ⇒ x#y

[f]take has a number of different actions depending on x. These include head (take from front of list), tail (take from back of list), common (take common elements), filter (take where function is non-zero), and select (take columns from table).

actionxy
headpositive intlist
tailnegative intlist
commonlistlist / dictionary
filterfunctionlist
selectname(s)table

Postive (negative) x returns the first (last) x elements of y. If abs[x]>count[y] then repeatedly pull from y.

 3#0 1 2 3 4 5          / take first 3 elements of a list
0 1 2

 10 # 17 12 25 / take repeat: take from list and repeat as needed
17 12 25 17 12 25 17 12 25 17

 -3#0 1 2 3 4 5         / take last elements of a list
3 4 5


If x and y are lists, then take returns any values common in both x and y. If x is a list and y is a dictionary, then take returns those values in y where the keys are in x.

 (1 2 3 7 8 9)#(2 8 20) / common
2 8

  (2 4 6 2 6 5 4) # (4 6 7 8 2 4 4 4) / substring of right argument
4 6 2 4 4 4
 
 (4 6 7 8 2 4 4 4) # (2 4 6 2 6 5 4)
2 4 6 2 6 4

 `b`d#`a`b`c`d!1 4 2 3
b|4
d|3

If x is a function, then select the values where the function is non-zero.

 (0.5<)#10 rand 1.           / select values greater than 0.5
0.840732 0.5330717 0.7539563 0.643315 0.6993048f

If x is a name or list of names, then take the name column or columns from the table.

 t:[[]x:`a`b`c;y:3 1 2;z:"tab"];t
x y z
- - -
a 3 t
b 1 a
c 2 b

 `y#t
y
-
3
1
2

 `z`y#t
z y
- -
t 3
a 1
b 2

 `x`z`y#t
x z y
- - -
a t 3
b a 1
c b 2

2.28 floor ⇒ _x

Return the integer floor of float x.

 _3.7
3

2.29 [f]drop ⇒ x_y

[f]drop has a number of different actions depending on x. This include de-head (drop from front of list), de-tail (drop from back of list), uncommon (drop common elements), and filter (drop where function is zero).

actionxy
deheadpositive intlist
detailnegative intlist
uncommonlistlist / dictionary
filterfunctionlist

Postive (negative) x drops the first (last) x elements of y.

 3_0 1 2 3 4 5          / drop first three
3 4 5

 -2_0 1 2 3 4 5         / drop last three
0 1 2 3

If x and y are lists, then drop returns any values from y not found in x. If x is a list and y is a dictionary, then drop returns those values in y where the keys are not in x.

 (1 2 3 7 8 9)_(2 8 20) / uncommon
,20

 `b`d_`a`b`c`d!1 4 2 3
a|1
c|2

If x is a function, then select values where the function is zero.

 (0.5<)_10 rand 1.           / values less than or equal to 0.5
0.02049699 0.1269226

2.30 string ⇒ $x

Cast x to string.

 $`abc
"abc"
 $4.7
"4.7"

2.31 parse ⇒ x$y

Parse string y into type x.

 `i$"23"
23
 `f$"2.3"
2.3
 `t$"12:34:56.789"
12:34:56.789
 `d$"2020.04.20"
2020.04.20

2.32 unique ⇒ ?x

Return the unique values of the list x. The ? preceeding the return value explicitly shows that the list has no repeated values.

 ?`f`a`b`c`a`b`d`e`a
?`f`a`b`c`d`e
 ?"banana"
?"ban"

2.33 [n]find ⇒ x?y

Find the first element of x that matches y otherwise return the end of vector.

 `a`b`a`c`b`a`a?`b
1
 `a`b`a`c`b`a`a?`d / missing thus return the list length
7
 0 1 2 3 4?10
5
 (1;`a;"blue";7.4)?3
4

Return random numbers. Documented at rand.

 3?1.
0.01640292 0.8029538 0.9569196

There is also another use of ? where x is a special name, eg. `js, `csv (or `cs), or `nv. This is further discussed in the I/O section.

2.34 type ⇒ @x

Return the data type of x. Lower-case represents a single element while upper-case represents a list of type indicated.

 @1
`i
 @1.2
`f
 @`a
`s
 @"a"
`c
 @2020.04.20
`d
 @12:34:56.789
`t
 @(1;1.2;`a;"a";2020.04.20;12:34:56.789)  / type of a list
`L
 @'(1;1.2;`a;"a";2020.04.20;12:34:56.789) / type of elements of the list
`i`f`s`c`d`t

2.35 [n]at ⇒ x@y

Given a list or function x return the value(s) at index(indices) y.

 (3 4 7 12)@2              / list
7

 `a`b`c@2
`c

 ((1 2);3;(4 5 6))@(0 1)   / values at indices 0 and 1
1 2
3

 {x*x}@3                   / function
9

 (sin;cos;1*)@\:0.01
0.009999833 0.99995 0.01

There is also another use of @ where x is a special name, eg. `js, `csv (or `cs), or `nv. This is further discussed in the I/O section.

2.36 value ⇒ .x

Evaluate a string of valid k code or list of k code. Can also be used to covert from a string to a numeric, temporal or name type.

 ."3+2"
5

 ."20*1+!3"
20 40 60

 .(*;16;3)
48

 n:3;p:+(n rand(+;-;*;%);1+n rand 10;1+n rand 10);p
% 6 3
* 2 7
- 5 5

 .'p
2 
14
0

 ."15"
15

 ."12.7"
12.7

 ."`abc"
`abc

2.37 dot ⇒ x.y

Given list x return the value at list y. The action of dot depends on the shape of y.

action@y#yexample
simple index`I1,2
simple indices`I1,1 3
recursive index`L10 2
recursive index over`L2(0 2;1 3)
 (3 4 7 12).,2
7
 `a`b`c.,2
`c
 x:(`x00`x01;`x10`x11`x12;`x20;`x30`x31`x32);x
x00 x01    
x10 x11 x12
x20        
x30 x31 x32

 x . ,1
`x10`x11`x12
 x . ,0 1 3
x00 x01    
x10 x11 x12
x30 x31 x32

 x . 3 1
`x31
 x . (1 3;0 1)
x10 x11
x30 x31

If x is a function then apply the function to y.

 (+). 3 2
5

 (+;-;*;%).\: 3 2
5  
1  
6  
1.5

 a:{x+y};b:{x*sin y};a . 3 1
4
 a . 3 1
4
 b . 3 1
2.524413

 (!).(`a`b`c;1 2 3)
a|1
b|2
c|3

Next: , Previous: , Up: Top