Day 1 ----- 0. e 1. atoms 2. vectors 3. lists 4. irregular atomic types 5. matrices: count, shape, rank 6. atomic extension 7. dictionaries 8. tables 9. null 10. general data-structures 11. variables 12. make/unmake dictionaries 13. the k tree 0. e start e: stevans-Mini:~ stevanapter$ e K 3.33/jdfygz0. 2021-12-01 (adam@1010) Copyright (C) 1993-2020 Kx Systems maa 8CPU stevans-mini 0 PROD 2025-01-31 1010data.com * #35010 exit e: stevans-Mini:~ stevanapter$ e K 3.33/jdfygz0. 2021-12-01 (adam@1010) Copyright (C) 1993-2020 Kx Systems maa 8CPU stevans-mini 0 PROD 2025-01-31 1010data.com * #35010 \\ stevans-Mini:~ stevanapter$ create and clear an error: 1 2 3+4 5 length error 1 2 3+4 5 ^ > \ create and clear multiple errors: 1 2 3+4 5 length error 1 2 3+4 5 ^ > 5 6+7 8 9 length error 5 6+7 8 9 ^ >> \ > \ 1. atoms type 4: examples ---- -- -------- int 1 10 0N 0I -0I float 2 2.3 2. 0n 0i -0i char 3 "x" sym 4 `abc ` 64 64 1234567891234j 0Nj 0Ij -0Ij 2. vectors type 4: examples empties ---- -- -------- ------- int -1 1 2 3 0#1 2 3 or !0 float -2 1 2 3.4 0#1 2 3 4. char -3 "abc" 0#"abc" or "" sym -4 `abc`defg`hi 0#`abc`defg`hi 64 -64 3 4 1234567891234j 0#3 4 1234567891234j 3. lists type 4: examples ---- -- -------- list 0 (1;"abc";`foo) 0 ,10 20 30 0 () 4:x is an expression which returns the type of x. x~y is true if x matches y, else false. lists of same-typed atoms collapse to vectors: (1;2;3)~1 2 3 (1.;2.;3.)~1 2 3. ("a";"b")~"ab" (`foo;`bar;`baz)~`foo`bar`baz n:v is assignment of the value v to n. n is called a 'variable'. a[i] is one-dimensional indexing: the i-th component of a. a[i]:v is one-dimensional indexed-assignment: replace the i-th component of a with v. lists can have lists and components: a:(1;2 3;(4 5 6;"abc")) a (1 2 3 (4 5 6 "abc")) a[0] 1 a[1] 2 3 a[2] (4 5 6 "abc") a[2;1] "abc" a[2;1;2] "c" a vector can explode to a general list: a:1 2 3 a[1]:2.5 a (1;2.5;3) a[1]:`a`b`c a (1 `a `b `c 3) and a general list can implode to a vector: a (1 `a `b `c 3) a[1]:100 a 1 100 3 4. irregular atomic types type 4: examples ---- -- -------- dict 5 a.b .() null 6 _n or *() lambda 7 {} {x+y} {[a;b]a+b} in k, functions are first-class, called 'lambdas'. nulls, lambdas and primitives like + can be treated just like other atomic datatypes. a:(12 13;+;;`abc;{x+y}) a (12 13 <- first component + <- second component <- third component (null) `abc <- fourth component {x+y}) <- fifth component a[1] + 4:a[1] 7 a[2] 4:a[2] 6 5. matrices: count, shape, rank #x is count of x, ^x is the shape of x. count: 1 = #10 3 = #10 20 30 2 = #(1 2 3;4 5 6) shape = dimensionality: 2 3 = ^(1 2 3;4 5 6) ,2 = ^1 2 !0 = 10 stop when the counts of levels disagree: 2 3 = ^((1 2 3;`a;`b`c);4 5 6) rank = # of dimensions: count of shape 2 = #^(1 2 3;4 5 6) 1 = #^1 2 0 = #^10 6. atomic extension atomic monads: $ % - f L -> M: apply f at every atom atomic dyads: % & * - + =< > L f a -> M a f L -> M L f M -> N if L and M are conformable examples: a + L: 1+2 3 4 3 4 5 L + a: 1 2 3+4 5 6 7 L + M: 1 2 3+4 5 6 5 7 9 L + M: 1 2 3+(4 5;6 7;8 9) (5 6 8 9 11 12) L and M are not conformable: 1 2+3 4 5 length error 1 2+3 4 5 ^ > indexing a list: m:10 20 30 m[0] 10 m[0 1] 10 20 m[2 0 1] 30 10 20 m[] 10 20 30 indexing a multi-dimensional list: m:(1 2 3;4 5 6) m[0;0 1] 1 2 m[0 1;1 2] (2 3 5 6) m[0] 1 2 3 m[;0] 1 4 7. dictionaries constructing a dictionary: . in a.x is punctuation, not a function. a.x:10 a.y:20 a.z:30 a .((`x;10;) (`y;20;) (`z;30;)) `x`y`z is called the domain of a. 10 20 30 is called the range of a. deconstructing a dictionary: dictionaries are indexed by elements of the domain: a[`x] 10 a[`x`y] 10 20 a[`z`y`z] 30 20 30 the domain of a dictionary: !a `x `y `z the range of a dictionary: a[] 10 20 30 dictionaries are atomic. we have multi-dimensional dictionaries: b.x.q:1 b.x.r:2 b.y.q:3 b.y.r:4 b.z.q:5 b.z.r:6 b .((`x .((`q;1;) (`r;2;)) ) (`y .((`q;3;) (`r;4;)) ) (`z .((`q;5;) (`r;6;)) )) b[`x] .((`q;1;) (`r;2;)) b[`x;`q] 1 b[;`q] 1 3 5 b[`x`y] (.((`q;1;) (`r;2;)) .((`q;3;) (`r;4;))) 8. tables a table is a dictionary whose variables are equal-length lists. t.f:10 20 30 t.g:`one`two`three t.h:12.2 13.3 14.4 t.i:100 200 300 t .((`f 10 20 30 ) (`g `one `two `three ) (`h 12.2 13.3 14.4 ) (`i 100 200 300 )) ^t[] 4 3 !t `f `g `h `i 9. null there is only one null, written: _n _n has no display! _n <-- nothing! _n is the value passed when indexing with empty brackets: m[_n] 10 20 30 a[_n] 10 20 30 10. general data-structures g.a.b:10 g.a.c:2 3 4 g.a.d.e:"abc" g.a.d.f:`a`b`c`d h:(1 2 3;g) h (1 2 3 .,(`a .((`b;10;) (`c 2 3 4 ) (`d .((`e;"abc";) (`f `a `b `c `d )) )) )) h is a list whose first component is a vector and whose second component dictionary of an (10), a vector (2 3 4) and a dictionary (d). h[0] 1 2 3 h[1;`a;`c;0 1] 2 3 11. variables a variable is a triple consisting of a name, a value, and an attribute dictionary. lists are aggregations of values. dictionaries are aggregations of variables. a .((`x;10;) (`y;20;) (`z;30;)) `x is the name of a variable in a. its value is 10. its attribute dictionary is null. we can add an attribute dictionary to a.x consisting of three variables p, q, and r: a.x..p:`foo a.x..q:`bar a.x..r:`baz a.x..p `foo a.x. .((`p;`foo;) (`q;`bar;) (`r;`baz;)) a .((`x 10 .((`p;`foo;) (`q;`bar;) (`r;`baz;))) (`y;20;) (`z;30;)) note that a.x. is a dictionary of three variables p, q, and r. each variable has a value (x=10, y=20, z=30) and each variable has an attribute dictionary. the attribute dictionaries of y and z are null. we can add an attribute dictionary to a.x..p consisting of a single variable k, whose value is 1 2 3 and whose attribute dictionary is null: a.x..p..k:1 2 3 a.x..p..k 1 2 3 a.x. .((`p `foo .,(`k 1 2 3 )) (`q;`bar;) (`r;`baz;)) 12. make/unmake dictionaries. up til now we've been creating dictionaries with dot notation and indexed assignment: a.b.c:10 a.b[`c]:100 but there is a more general, functional form for creating a dictionary: a:((`a;10);(`b;20);(`c;30)) d:. a d .((`a;10;) (`b;20;) (`c;30;)) . is a function which takes a list of symbol-value pairs and returns a dictionary. the display of a dictionary is, like all other displays in k, *round-trip*: e:.((`a;10;) > (`b;20;) > (`c;30;)) e~d 1 . is its own inverse: applied to a dictionary it returns a three-column list: . d ((`a;10;) (`b;20;) (`c;30;)) namely, exactly the list which, when . is applied to it, returns the original dictionary: d~.. d 1 . is called "make dictionary" when applied to a list of the appropriate kind, and "unmake dictionary when applied to a dictionary. 13. the k tree all computations take place in the k workspace. the k workspace is structured as a tree. the root of the tree is nameless. at any point you are located in a directory on the tree which is called "the current directory", and which you can determine with the command \d: stevans-Mini:~ stevanapter$ e K 3.33/jdfygz0. 2021-12-01 (adam@1010) Copyright (C) 1993-2020 Kx Systems maa 8CPU stevans-mini 0 PROD 2025-01-31 1010data.com * #35010 \d .k the .k directory is one level down from the root. a truly remarkable fact about the k tree is that it is a dictionary! here is a sequence which shows that .k can be queried with ! and assigned to a variable just like any other dictionary: a:10 b:20 !.k `a `b .k .((`a;10;) (`b;20;)) q:.k !.k `a `b `q .k .((`a;10;) (`b;20;) (`q .((`a;10;) (`b;20;) (`q;;)) )) the \d command can be used to navigate the k tree: \d . !k `a `b `q the .k directory is just a dictionary in the root. here we create a dictionary a containing a dictionary b, then navigate into a, then into b, then back up to a, and finally back up to .k: a.b.c:10 a.b.d:20 a .,(`b .((`c;10;) (`d;20;)) ) \d a b .((`c;10;) (`d;20;)) \d b c 10 d 20 \d ^ b .((`c;10;) (`d;20;)) \d ^ a .,(`b .((`c;10;) (`d;20;)) ) finally, since every variable in a dictionary has an attribute dictionary, let's create one in b and navigate to it: a.b.c..foo:1 2 3 a.b.c. .,(`foo 1 2 3 ) \d a.b.c. foo 1 2 3