Skip to content
Tom Szczesny edited this page Apr 1, 2018 · 10 revisions

A quick and very basic tour of Kona. (The User and Reference Manuals for K 2.0 from Kx Systems are at "Links" on the wiki.)

1. Vector

Write some numbers in a row and you create a vector

   14 5 6
 14 5 6

You can also create a vector with the comma ("join") operator

   2, 3, 4
 2 3 4

A one-element integer vector is made like this

    ,2
  ,2

2. Vector generation

You can create a vector like 0 1 2 3 4 ... using an exclamation point ("enumerate")

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

Use _draw to create a random vector. Creates n numbers < m

   4 _draw 10
 7 2 9 0

3. Vector modification

Let's examine the control flow in K. Programs are interpreted row by row, right to left. That means the order of the operators counts, but perhaps not in the way you would expect.

   2+2*2
 6
   2*2+2
 8

It is very easy to reverse a vector:

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

As you can see, the first command was !10 and it created 10 integers. Then it was reversed by a vertical line. There are a lot of one-symbol vector modification commands. I'll show you some of the simplest ones.

Minus sign ("negate"):

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

Invert logically:

   randomNumbers: 10 _draw 2
 1 1 0 1 1 1 0 0 0 1
   ~ randomNumbers
 0 0 1 0 0 0 1 1 1 0

For integers x, the logic is:

  • ~x returns 0 when x is non-zero
  • ~x returns 1 when x is zero

4. Arithmetic

   2 + 2
 4
   3 * 3
 9
   9 % 4 / huh, that's division!
 2
   1 - 3
 -2

Spaces are not necessary. Also, you can change the priority of operations by using parentheses:

   2*2+2
 8
   (2*2)+2
 6

5. Vector arithmetics

The four basic arithmetic operations work also with vectors. Let's examine all the possible combinations:

  1. singleone - singleone
  2. singleone - vector (Kona also allows vector - singleone)
  3. vector - vector (length must be the same!!!)

SingleOne - Vector examples

   -5 - 1 2 3 4 5
 -6 -7 -8 -9 -10
   10 * !10	/ multiply each element by 10
 0 10 20 30 40 50 60 70 80 90
   10 * |!10	/ multiply each element of reversed vector by 10
 90 80 70 60 50 40 30 20 10 0
   10 * ~|!10	/ multiply each element of negated reversed vector by 10
 0 0 0 0 0 0 0 0 0 10
   10 * ~(-5 + |!10) / and so on...
 0 0 0 0 10 0 0 0 0 0

Vector-Vector examples

   1 2 3 + 3 2 0
 4 4 3
   1 2 3 + !3
 1 3 5
   !3 + !2
 (0 0
  0 1
  0 2
  0 3
  1 0
  1 1
  1 2
  1 3
  2 0
  2 1
  2 2
  2 3)
   (!3) + !3
 0 2 4

As you can see, expressions may be (and must sometimes be) wrapped in parentheses. Remember, right to left:

   !2			/ generate a list
 0 1
   3 + 0 1		/ simple addition
 3 4
   ! 3 4		/ enumerate a vector!
 (0 0			/ result is a vector of permutations of the numbers in the original
  0 1
  0 2
  0 3
  1 0
  1 1
  1 2
  1 3
  2 0
  2 1
  2 2
  2 3)

Summary

This is a quiz to ensure you understand the K language.

(An arithmetic progression is an incrementing sequence like 0,1,2,3,... )

  1. Clear (make zero) first, third, fifth and 7-th numbers of arithmetic progression (size 10) using a logic mask.
  2. Create arithmetic progression with a starting number -5 and step 4.
  3. Add an arithmetic progression to the reverse of itself. You've got a series of the same numbers. Can you use that method to create a vector of 21 sevens? Can you do the same using one of the special cases of the ? or _draw randomizer? :)
  4. a bit more complex...
  5. create vector (named 'myRandom') of random non-zero integers (ten is enough)
  6. clear (make zero) the third element of vector "myRandom"
  7. write 100000 on that cleared place