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      unary
+  +      flip
-  -      -
*  *      first
%  div    sqrt
!  mod    key
&  &      where
|  |      reverse
<  <      asc
>  >      desc
=  =      freq
~  ~      ~
,  ,      ,
#  take   count
_  drop   floor
^  cut    sort
@  @      type
?  find   distinct
\$  parse  str
.  dict   value
```

### 2.1 flip ⇒ +x

Flip, or transpose, x where x is not a simple element.

``` 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.2 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:01

10:00:01

10:00:00.001
```

### 2.3 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.4 minus ⇒ x-y.

Subtract y from x.

``` 5-2
3

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

### 2.5 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.6 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.7 sqrt ⇒ %x

Return the square root of x.

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

### 2.8 divide ⇒ x`%`y

Divide x by y.

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

### 2.9 key ⇒ !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

B:`b'+!16#2 / create the binary numbers from 0 to 65,535

B[12123]     / take the 12123 element
0 0 1 0 1 1 1 1 0 1 0 1 1 0 1 1

2/B[12123]   / convert back to decimal
12123
```

### 2.10 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.11 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.12 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.13 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.14 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.15 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.16 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.17 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"
a|3
b|1
n|2

=0 1 0 2 10 7 0 1 12
0|3
1|2
2|1
7|1
10|1
12|1
```

### 2.18 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.19 not ⇒ ~x

Boolean invert of x

``` ~1
0

~1 0 1
0 1 0

~37 0 12
0 1 0
```

### 2.20 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.21 enlist ⇒ ,x

Create a list from x

``` ,3
,3

,1 2 3
1 2 3

3=,3
,1

3~,3
0
```

### 2.22 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.23 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.24 [f]cut ⇒ x^y

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

 action x rows positive int columns negative int indices list function function table column

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.25 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.26 [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).

 action x y head positive int list tail negative int list common list list / dictionary filter function list select name(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.27 floor ⇒ _x

Return the integer floor of float x.

``` _3.7
3
```

### 2.28 [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).

 action x y dehead positive int list detail negative int list uncommon list list / dictionary filter function list

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.29 str ⇒ \$x

Cast x to string.

``` \$`abc
"abc"
\$4.7
"4.7"
```

### 2.30 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.31 distinct ⇒ ?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.32 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.33 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.34 [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.35 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.36 [f]dot ⇒ x.y

Return the value(s) of list x at list y. This differs from at as dot can recursively index.

• Simple index returns the value(s) at x at each index y, i.e. x@y@0, x@y@1, ..., x@y@(n-1).
• Recursive index returns the value(s) at x[y@0;y@1].
• Recursive index over returns x[y[0;0];y[1]], x[y[0;1];y[1]], ..., x[y[0;n-1];y[1]].
 action @y #y example simple index `I 1 ,2 simple indices `I 1 ,1 3 recursive index `L 1 0 2 recursive index over `L 2 (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

(:).(`a;12 14 15)
a
12 14 15
```

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