0. the elements of k start k from the command line explore 'help' with \ exit from k with \\ 1. k is a small language. 7 atomic datatypes 4 vector datatypes 1 general list 20 verbs ~!@#$%^&*-_=+|:<,>.? 6 adverbs / \ / /: \: ': (see section 7 for details) assignment n:v composition 2+3* #!: projection f[x]y f[;y]x nominalization (+) punctuation: ( ) { } [ ] ` " ; controls: :[x;y;z;..] if[x;..y..] do[x;..y..] while[x;..y..] ' signal : return k executes right to left but reads left to right. 2. three parts of speech. syntax semantics ------ --------- noun data verb f1 = first-order function (data <- data) adverb f2 = second-order function (f1 <- f1 | data) 3. data types. atomic compound ------ -------- 1 int -1 int vector 2 float -2 float vector 3 char -3 char vector 4 sym -4 sym vector 5 dict 6 null (_n) 7 lambda 99 64-bit -99 64-bit vector 0 general list a vector is a list all of whose elements are atoms of the same type. each vector type has a special notation. (1;2;3) 1 2 3 4. and .. system functions: _in _lin .. system variables _v _d _i .. commands: \l \d .. file i/o: 0: 1: 2: 6: .. ipc: 3: 4: the k tree (see section 8 for details) 5. application and indexing. one dimensional indexing: v:10 20 30 40 50 v 0 2 / equivalent forms: v[0 2], v@0 2, v .,0 2 20 30 v 2 0 2 30 20 30 v 2 3#0 2 4 (10 30 50 10 30 50) one dimensional application f:{x+1} / equivalent forms: f[0 2], f@0 2, f .,0 2 f 0 2 1 3 f 2 0 2 3 1 3 f 2 3#0 2 4 (1 3 5 1 3 4) multi-dimensional indexing and application v:2 3#!6 f:{x+y} v[i;j] f[a;b] equivalent forms: v .(i;j), f .(a;b) multidimensional indexing and application extend to n-dimensional forms. 6. tables, selection, sorting. t:.+(`f`g`h;3 100_draw 10) / 3 x 100 table 6.1. selection. t.f>2 / 1 iff t.f>2 &22 t[;&22 @[t;_n;@[;&22 @[t;_n;@[;&(2t.g)&6=t.h]] / table where x&y&z i:&2t.g i i@:&6=t.h i j:&(2t.g)&6=t.h / non-converging i~j / match! 10 20 30 _n / _n is all _n 10 20 30 / _n is the identity function f:{x y z x} / x indexed by y applied to z indexed by x k:f/[_n;(&2<;&9>;&6=);t`f`g`h] / converging select using f i~k / match! @[t;_n;@[;k]] / selected rows 6.2 sorting. atom atom+list -> list list+atom -> list list+list -> list there are relatively few adverbs. 'each' provides limited atomic extension for non-atomic functions: a+b = a+'b 'each-left' and 'each-right' are useful special cases of 'each'. 'over' abstracts from the pattern of repeatedly modifying a single object with its previous state. 'scan' provides the intermediate results of 'over'. 'prior' applies a dyadic function to overlapping pairs in a list. atom+list -> /: list+atom -> \: list+list -> ' 7.1. each. atomic functions pervade lists: x+y*z 'each' is used to pervade non-atomic functions. f'x f''x f'''x a f'b f'[a;b;c;d] the general form: f'[x;y;..;z] all list arguments must have the same count atomic argumens are extended to the count of the first list (if any) e.g. f'[10 20 30;40;50 60 70] 7.2. each-left and each-right. a f\:b = f[b]'a / hold the right argument constant and iterate over the left argument a f/:b = f[a]'b / hold the left argument constant and iterate over the right argument 7.3. over. valence > 1 the general form: f/[x;y;..;z] x is a blob y .. z are lists count n f/[x;y;..;z] = x:f[x;y 0;..;z 0] x:f[x;y 1;..;z 1] : x:f[x;y n;..;z n] alternatively: f[..f[f[x;y 0;..;y n];y 1;..;y 1]..;x n;..;y n] example: f:{x+y*z} f/[100;10 20 30;40 50 60] 3300 valence = 1 the general form: f/x iterate f on x until the result is the same twice in a row or matches the initial value. (0|-1+)/10 0 (\1!)/"abc" "bca" "cab" "abc" "cab" valence = 2 has an optional syntactic form: +/[2;1 2 3] / standard form 8 2+/1 2 3 / initial argument to the left of +/ 8 +/2 1 2 3 / use the first element as the initial argument 8 "raze": join a list of lists and atoms: ,/("abc";"defg";"x") "abcdefgx" john earnest adds: +/3 7 18 4 9 can be thought of as: 3+7+18+4+9 or more precisely as: (((3+7)+18)+4)+9 in other words, +/ derives a verb which, when applied to 3 7 18 4 9, gives the behavior: (((3+7)+18)+4)+9 7.4. scan. scan syntax is just like over: valence > 1: f\[x;y;..z] valence = 1: f\x valence = 2: f\[x;y], x f\y, f\x think of f/x as returning the last of f\x, or as f\x as returning all the intermediate results of f/x. +/!3 2 +\!3 0 1 2 (1!)/"abc" "cab" (1!)\"abc" ("abc" "bca" "cab") boolean scans: &\1 1 1 0 1 0 1 / turn off all 1's after the first 0 1 1 1 0 0 0 0 7.5. examples combining ' / \ /: and \: left-justify a string: {(+/&\x=" ")_ x}" strip leading blanks" "strip leading blanks" {(+/&\x=" ")!x}" rotate leading blanks" "rotate leading blanks " parity-check = find quoted substrings: {(~=)\x="'"}"this 'has quoted' substrings 'embedded' in it" 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 {x@&(~=)\x="'"}"this 'has quoted' substrings 'embedded' in it" "'has quoted'embedded" check for unbalanced parentheses: unb:{~(*|=/b)&~|/) (`y;`foo;))) / (`y;`foo;) observe that a's attribute dictionary has two variables x and y. each one follows the pattern of being a triple (n;v;a). variables on the k tree are recursive in one-and-a-half (!) directions: - down, e.g. .k.a.b.c.d - to the right, e.g. .k.a..x..y..z variables in an attribute directory can have attributes. 10. help \ \0 data \+ verbs \' adverbs \_ system verbs and nouns \. assign, define, control and debug \: i/o, dynamic load and client/server \` os commands, dialog boxes \a attributes, dependencies and triggers \g gui attributes \l f load script f.k \s f step script f.k \w workspace used, allocated, mapped \c [0|1] console [off|on] \e [0|1] error flag [off|on] \b [t|s] break flag [trace|stop|none] \d [d|^] k directory [go to] \v [d|^] variables [directory] \i [v] invalid [antecedents/dependents] \p [n] print precision [digits] \t [x] time [x] milliseconds \r [s] random seed \cd [d] O/S directory [go to] \[other] O/S execute \\ exit \(escape) ctrl-c(stop)