Next: , Previous: , Up: Top  


4 Verb

This chapter covers verbs which are the core functions of k9. Given how different is it to call functions in k9 than many other languages this is probably a chapter that will have to be covered 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 and type of arguments. This reuse of symbols is also an item that causes confusion for 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
: x,        set.
+ flip,     plus.
- negate,   minus.
* first,    times.
%           divide.
& where,    min/and.
| reverse,  max/or.
< asc,      less.
> dsc,      more.
= group,    equal.
~ not,      match.
! enum,     key.
, enlist,   cat.
^ sort,  [f]cut. 
# count, [f]take.  
_ floor, [f]drop.    
$ string,   cast+.    
? unique+,  find+.    
@ type,  [f]at.    
. value, [f]dot.

4.1 x ⇒ :x

4.2 set ⇒ x:y

Set a variable, x, to a value, y.

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

4.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

4.4 plus ⇒ x+y

Add x and y.

 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, will error :length
: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

4.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

4.6 minus ⇒ x-y.

Subtract y from x.

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

4.7 first ⇒ *x

Return the first value of x. Last can either be determine by taking the first element of the reverse list (*|‘a‘b‘c) or using last syntax ((:/)‘a‘b‘c).

 *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

4.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

4.9 divide ⇒ x%y

Divide x by y.

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

4.10 where ⇒ &x

Given a list of binary values return the indices where the value is non-zero. 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.

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

4.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

4.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

4.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

4.14 asc(dsc) ⇒ < (>) x

Sort a list or dictionary in ascending (<) or descending (>) order. Applied to a list will return the indices to be used while a dictionary is directly sorted by value

 <2 3 0 12
2 0 1 3

 x@<x:2 3 0 12
0 2 3 12

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

 <d
c|1
b|2
a|3

4.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

4.16 group ⇒ =x

A dictionary of the disinct values of x (key) and 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    

4.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

4.18 not ⇒ ~x

Boolean invert of x

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

4.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

4.20 enum ⇒ !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

Given a list of integers, x, generate a list of lists where each individual index goes from 0 to n-1. Aka an odometer where the each place can have a separate base and the total number of lists is the product of all the x values.

 !2 8 16  / an odometer where the values are 0-1, 0-7, and 0-15.
0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0 0 0 0 0 0 0 0 0 0 0  0  0
0 0 0 0 0 0 0 0 0 0  0  0  0  0  0  0 1 1 1 1 1 1 1 1 1 1  1  1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 8 9 10 11
 12#+!2 8 16 / flip the output and display first 18 rows
0 0  0
0 0  1
0 0  2
0 0  3
0 0  4
0 0  5
0 0  6
0 0  7
0 0  8
0 0  9
0 0 10
0 0 11
 5_+!2 8 16 / flip the output and display last 5 rows
1 7 11
1 7 12
1 7 13
1 7 14
1 7 15
 B:`b$+!16#2 / create a list of 16-bit binary numbers from 0 to 65535
 B[12123]    / pull the element 12,123
0010111101011011b
 2/:B[12123] / convert to base10 to check it's actually 12123
12123

4.21 key ⇒ x!y

Dictionary of x (key) and y (value). If looking to key a table then refer to [f]cut.

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

4.22 enlist ⇒ ,x

Create a list from x

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

4.23 cat ⇒ x,y

Concatenate x and y.

 3,7
3 7
 "hello"," ","there"
"hello there"

4.24 sort ⇒ ^x

Sort list or dictionary x into ascending order. Dictionaries are sorted using the keys.

 ^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

4.25 [f]cut ⇒ x^y

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

 3^!10
0 1 2
3 4 5
6 7 8

 0 1 5^!10
0        
1 2 3 4  
5 6 7 8 9

 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

 {(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

Cut into domain and range, aka key a 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

 (0#`)^kt                  / unkey the keyed table
a b 
- --
x  1
y 20
z  1

4.26 count ⇒ #x

Count the number of elements in 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

4.27 [f]take ⇒ x#y

Take is used to return a subset of list y depending if x is a atom, list, or function. If x is an atom, then postive (negative) x returns the first (last) x elements of y. If x is a list, then returns any values common in both x and y. If x is a function (f), then filter out values where the funtion is non-zero.

 3#0 1 2 3 4 5          / take first
0 1 2
 -3#0 1 2 3 4 5         / take last
3 4 5
 2#"hello"
"he"
 (1 2 3 7 8 9)#(2 8 20) / common
2 8
 (0.5<)#10?1.           / filter
0.840732 0.5330717 0.7539563 0.643315 0.6993048f

4.28 floor ⇒ _x

Return the integer floor of float x.

 _3.7
3

4.29 [f]drop ⇒ x_y

Drop is used to remove a subset of list y depending if x is a atom, list, or function. If x is an atom, then postive (negative) x removes the first (last) x elements of y. If x is a list, then remove any values in x from y. If x is a function (f), then remove values where the funtion is non-zero.

 3_0 1 2 3 4 5          / drop first
3 4 5
 -3_0 1 2 3 4 5         / drop last
0 1 2
 2#"hello"
"he"
 (1 2 3 7 8 9)_(2 8 20) / drop common
,20
 (0.5<)_10?1.           / drop true
0.4004211 0.2929524f

4.30 string ⇒ $x

Cast x to string.

 $`abc
"abc"
 $4.7
"4.7"

4.31 cast+ ⇒ x$y

Cast 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

Multiply matrices x and y together.

 (1.*!3)$(3.+!3)
14.

(0 1 2;3 4 5;6 7 8)$(10 11;20 21;30 31)
 80  83
260 272
440 461

4.32 unique+ ⇒ ?x

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

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

4.33 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
7
 0 1 2 3 4?10
5
 (1;`a;"blue";7.4)?3
4

Generate x random values from 0 to less than y (int, float, time, bit) or from y (list of characters or names). Random dates can be generated by using random ints and adding to the start date.

 3?5
0 0 2
 3?5.
2.238258 3.038515 0.7879856
 3?12:00:00
^05:55:27 09:46:18 10:49:18
 3?2b
001b
 3?"AB"
"BBA"
 3?`a`b`c`d
`d`c`a
 min 2020.01.01+1000?366
2020.01.01
 max 2020.01.01+1000?366
2020.12.31

4.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

4.35 [f]at ⇒ x@y

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

 (3 4 7 12)@2
7
 `a`b`c@2
`c
 ((1 2);3;(4 5 6))@(0 1)   / values at indices 0 and 1
1 2
3

If x is a function then determine the function with (at) pararmeter y.

 {x*x}@3
9

4.36 value ⇒ .x

Value a string of valid k code or list of k code.

 ."3+2"
5

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

 .(*;16;3)
48

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

 .'p
2 
14
0 

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