Working in a single language reduces errors, increases
speed yet further, and is more fun because you can
concentrate on the algorithm.
(Note: Many errors and much overhead comes from integrating different
languages, e.g. C/Perl/Sybase/GUI.)
k
"a"
"Hello, world!"
`Hello `"Hello, world!" / need the quotes because of the space
15 16.2
"a" `fun 2 3.2
4 2 5 67 2 (4;2; 5; 67; 2) / equivalent to the above `good `better `best (`good; `better; `best) / equivalent to the above "good" / a sequence of characters is a vector ("g";"o";"o";"d") / a sequence of characters is a vector
("hello"; `hello; 15; `abc)
x: 3 5 2 17 2 x[0] x[3]
x , x
5 ,5
comp.name: `zurich `aetna `"met life" comp.assets: 43 23 35 `show $ `comp
For example, vertical bar is max when dyadic and reverse when used monadically.
4 | 5 32 64 | 21 89 | 32 64 21 89
Another example occurs with asterix.
4 5 * 2 3 / multiplication * 4 5 2 3 / first
"foobar" 0: "Hello, world!"
The 0: (zero colon) operator writes character vectors to the file. Lists of charactor vectors are separated by a newline. Try an example on your own.
One can also read in a file, but in that case the file name is on the right side.
a: 0: "foobar" / comes back as list whose zeroth element is string
"foosymb" 1: `"Hello, world!" / write a symbol to a file
Reading is a bit more subtle. NT's treatment of memory mapped files causes us to make the following rule:
If your program writes a file, your program should use 2: when reading the file (in version 2.2 and later). If your program only reads a file, then use 1: to read because 1: uses memory mapping whereas 2: reads the data.
In our case, we have written to foosymb, so we have to read using 2:.
asymb: 2: "foosymb"
"foo" 5: (`op1; "arg1"; `arg2)file "foo" will be extended by three more elements. But this is not good since the end of this operation does not have a clear boundary.
Therefore, it is better to say
"foo" 5: ,(`op1; "arg1"; `arg2)because that appends a single element consisting of the list
(`op1; "arg1"; `arg2).
k foo abc d efwill put into the variable _i within the foo program the strings "abc", "d", and "ef".
x: !100
Generate 1000 numbers between 0 and 18 with replacement.
a: 1000 _draw 19Generate 19 numbers between 0 and 18 without replacement. ( Warum nur neunzehn?)
a: 19 _draw -19 / generate 1000 numbers between 0 and 18 without replacementOne more kind of random number: random numbers between 0 and 1.
b: 100 _draw 0
5 + 3 5 6 7 - 10 20 30 5 6 7 * 3 6 7 8 9 % 2
Ok, you may not be used to having the percent sign be used for division, but you'll learn to like it.
4 > 2 4 6 > 2 8 (!10) > 7 4 > !10
\e 1 \b t
_tthat gives the number of seconds after Jan 1, 2035, which is a Monday. (So, this number is currently negative.)
_ltime _t
_gtime _t
* _gtime
days: `Mon `Tues `Wed `Thu `Fri `Sat `Sun days @ (_ _t%86400) ! 7
\_
var:{[x] a: avg[x*x] b: avg[x] a - ( b * b)}
Try this again when you have a vector of discounts, because the discount rate varies with the yield curve.
We are told the following facts:
2.1a + 3b + 4c = 30.5
1.1a + b + c = 10.5
4.1a + 3b + 2c = 40.5
Your job is to find a, b, and c.
n: 100 x: (n*n) _draw 10000 m1: (n;n) # x / note that n n # x won't work because n n is function app x: (n*n) _draw 10000 m2: (n;n) # x dot:{[x;y] +/x*y} r: m1 dot/:\: +m2 / dot product with the transpose.
/:\:is important. Reversing the order gives the transpose. This order causes dot to multiply the top row times all the columns first (forming the first row of the result).
seq1: "abcde" seq2: "012" seq1 ,\: seq2
seq1: "abcde" seq2: "012" seq1 ,/: seq2
seq1: "abcde" seq2: "012" seq1 ,\:/: seq2 / note contrast with seq1 ,/:\: seq2 / This makes seq2 vary more frequently than seq1.
presentval:{[a; d; e] +/ (a%(d^e))} amount: 100 125 200 150 117 / income stream discountrate: 1.05 / compounding rate exponent: 0 0.5 1 1.5 2 / how many years out presentval[amount;discountrate;exponent]
pvgeneral:{[a; d] +/ a * d} amounts: 100 125 200 150 117 / income stream discounts: 1 0.9 0.8 0.7 0.55 pvgeneral[amounts;discounts]
A: (2.1 3 4; 1.1 1 1; 4.1 3 2) / For the purpose of this function, / each list indicates a column. / Since we want them to be rows, flip the matrix. A: +A y: (30.5 10.5 40.5) x: y _lsq A / Check that this works by typing +/A * x
/ given a file, take out those lines that / are betweenand/ where those strings are at the beginning of a line. a: 0: _i[0] out: () printit: 0 decide: {[line] if[line ~ "" printit:: 1 :() ] if[line ~ "" printit:: 0 :() ] if[printit = 1 out,: ,line ] } decide'a str: (_i[0]),"out.k" str 0: out