Next: , Previous: , Up: k9: Manual  


2 Verbs

This chapter covers verbs which are the core primitives of k9. Given how different it is 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!)

Verb     monad   
+ +              
- -              
* *              
% div            
! mod    where
& &      flip  
| |      reverse
< <      asc
> >      desc
= =      freq
~ ~      ~       
, ,      ,
# take   count
_ drop   first
^ cut    sort
@ @      type
? find   unique
$ parse  str
. dict   value

2.1 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.2 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.3 minus ⇒ x-y.

Subtract y from x.

 5-2
3

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

2.4 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.5 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.6 sqrt ⇒ %x

Return the square root of x.

 %25
5.000e+00
 %14.4
3.794e+00

2.7 div ⇒ x%y

Divide x by y.

 12%5
2.400e+00

6%2
3.000e+00

2.8 mod ⇒ x!y

The remainder after y divided by x using integer division. x and y must be integers.

 12!27
3
 5!22
2

2.9 where ⇒ !x

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

 !00110011b
2 3 6 7

 "banana"="a"
010101

 !"banana"="a"
1 3 5

 x@!30<x:12.7 0.1 35.6 -12.1 101.101
3.560e+01 1.011e+02

2.10 flip ⇒ &x

Flip, or transpose, x where x is list of 2 or more dimensions.

 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 6}

 &`a`b.&x
[a:1 3 5;b:2 4 6]

Given a list of boolean values return a list of indices where the value is zero (index 0) and non-zero (index 1).

 &00110011b
(0 1 4 5;2 3 6 7)

 "banana"="a"
010101

 &"banana"="a"
(0 2 4;1 3 5)

 x@&30<x:12.7 0.1 35.6 -12.1 101.101
(1.270e+01 0.100e+00 -1.210e+01;3.560e+01 1.011e+02)

Given a list of non-binary values, return the ascending sorted indices.

 & 4 1 2 -3 4 1 
(,3;1 5;,2;0 4)

 &`a`b`a`d
(0 2;,1;,3)

 &"blue red greeen"
(4 8;,0;,7;3 6 11 12 13;,9;,1;,14;5 10;,2)

 & 2023.01.01 2023.01.02 2023.01.01
(0 2;,1)

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

0 1 0 0 1 0&1 1 1 0 0 0
0 1 0 0 0 0

 `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

 1 0 1 1 0 1|0 0 0 1 1 1
1 0 1 1 1 1

 |/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
1 1 1

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

 "a"<"b"
1b

2.16 freq⇒ =x

Freq takes a list of uniform type, x, and returns a dictionary of the distinct values (key) and their number of occurences (value).

 ="banana"
abn.3 1 2

 =0 1 0 2 10 7 0 1 12
0 1 2 7 10 12.3 2 1 1 1 1

2.17 equal ⇒ x=y

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

 2=2
1

2=3
0

2=2.
1

 "banana"="abnaoo" / check strings of equal length by character
0 0 1 1 0 0

 "banana"="apple"  / unequal length strings error
!length

2.18 not ⇒ ~x

Boolean invert of x

 ~1
0

 ~1 0 1
0 1 0

 ~37 0 12
0 1 0

2.19 match ⇒ x~y

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

 2~2
1

 2~3
0

 2~2.
0

 "banana"~"apple"
0

 `a`b~`a`b  / different than = which is element-wise comparison
1

 `a`b=`a`b
1 1

 f:{x+y}
 f~{x+y}
1

2.20 enlist ⇒ ,x

Create a list from x

 ,3
,3

,1 2 3
1 2 3

 3=,3
,1

 3~,3
0

2.21 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.22 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.23 [f]cut ⇒ x^y

Cut list y by size, indices, or function x. Also, cut table y into key x and value.

actionx
rowspositive int
columnsnegative int
indiceslist
functionfunction
tablecolumn

Positve integer x, cut y into x rows

 3^101+!18   / 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+!18  / 3 columns
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

Table returning utable (aka keyed table).

 t:[[]a:`x`y`z;b:1 20 1];t / an unkeyed table
a b 
- --
x  1
y 20
z  1

 kt:`a^t;kt                / set `a as the key
a|b 
-|--
x| 1
y|20
z| 1

2.24 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                      / single element is 1
1

2.25 [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?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.26 floor ⇒ _x

Return the integer floor of float x.

 _3.7
3

2.27 [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 two
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?1.           / values less than or equal to 0.5
0.02049699 0.1269226

2.28 str ⇒ $x

Cast x to string.

 $`abc
"abc"
 $4.7
"4.7"

2.29 parse ⇒ x$y

Parse string y into type x.

 `i$"23"
23

 `f$"2.3"
2.3

 `f$"2"
2

 `t$"12:34:56.789"
12:34:56.789

`d$"2020.04.20"
2020.04.20

2.30 unique ⇒ ?x

Return the distinct 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.31 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 x uniform random numbers between 0 and y or from list y with the same type as y. If x is negative then values are unique.

 3?1.      / values [0,1.)
0.01640292 0.8029538 0.9569196

 3?10      / values [0,9)
6 9 4

 5?`a`b`c  / sample from list y
`a`a`c`c`b

 -3?3      / unique values
1 2 0

 3?3       / duplicates are possible
2 2 1

2.32 type ⇒ @x

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

 @2
`i

 @1.2
`f

 @`a
`n

 @"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
`:

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

 @{x*x}
`.

 @`a`b!1 2
`a

 @[[]x:`a`b;y:1 2]
`A

 @[x:`a`b]y:1 2]
`a

2.33 [f]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

 (sqr;sqrt;|;#)@'(3;10;"word";`a`b`c) / list of fuctions applied to list of inputs
9       
3.162278
drow    
3       

2.34 value ⇒ .x

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

String of k code

 ."3+2"
5

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

List of k code

 .(*;16;3)
48

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

 .'p
2 
14
0

 .(:;`a;12 14 16) / set a to (12 14 16)
 a
12 14 16

System command (must be prefixed with \\)

 ."\\ls ."
Mi2.0     
json.dylib

Convert from string to numeric or name.

 ."15"
15

 ."12.7"
12.7

 ."`abc"
`abc

2.35 dict ⇒ x.y

Create a dictionary from key x and value y. Keys and vlaues can be of the various types, eg. numbers, symbols, characters, strings, dates, and times. There is a an alternative form to create dictionaries using curly brack notation, eg. {a:2 3}.

 d:`a`b`c.1 2 3;d
`a`b`c.1 2 3

 d`b
2

 d:1 2 3 . 4 5 6;d
1 2 3.4 5 6

 d:"blue" . 1 2 3 4;d
blue.1 2 3 4

 d:2023.01.01 2023.02.01 2023.03.01 . 1 2 3;d
2023.01.01 2023.02.01 2023.03.01.1 2 3

 d:`a`b`c . (1 2 3;4 5;6);d
{a:1 2 3;b:4 5;c:6}

 fa[a] a+1
 fb[a] a+2
 fc[a] a+3

 d:`f1`f2`f3 . (fa;fb;fc);d
{f1:(,`a;0a+;(1); a+1);f2:(,`a;0a+;(2); a+2);f3:(,`a;0a+;(3); a+3)}

 (d@`f1)@99
100

(d@`f3)@99
102

Next: Adverbs, Previous: Introduction, Up: k9: Manual