Skip to content

Instantly share code, notes, and snippets.

@razetime
Last active October 8, 2021 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save razetime/f6713f488bc26a3b618e85ba0f8069de to your computer and use it in GitHub Desktop.
Save razetime/f6713f488bc26a3b618e85ba0f8069de to your computer and use it in GitHub Desktop.

K tutorial

Welcome to K. K is an array-based programming language. Although this term may sound new to many programmers, K comes from a lineage of programming languages which date back to the 1960s.

It is the 1960s. Enter APL. APL originally started out as a general math notation, made to standardise the way mathematical expressions can be looked at. With the creation of APL's revolutionary math notation came the realization that it was very effective at describing algorithms, making it very effective with consistently communicating algorithmic, math-oriented thought. This original award winning idea eventually became what is known today as the APL programming language.

Array languages to this day are still a rather niche area, since they are very unlike what people are taught as beginner programmers. Hence, I'd like to request all viewers to maintain an open mind and have fun.

K is an APL-style array language which aims to eliminate many of APL's problems in programming. At a glance, the most noticeable improvements are:

  • You can type K on an ordinary QWERTY keyboard with no extra software.
  • K is built more toward programming than math, and hence simplifies the steep learning curve of APLs more math oriented sides.
  • K adds and removes features on every iteration of its design. This lets it refine its design better than languages which are forced to maintain backward compatibility, like APL.

Each one of K's major iterations is called a dialect. In this tutorial, we will be learning about ngn/k, an open source version of the K6 dialect.

K takes some time and effort to learn, just like any language. Some part of K will be hard to digest coming from an imperative language background, but with a little practice and an open mind, I can assure you that you will be able to do great things with K.

To start with K, you have to first understand right to left evaluation. Take this simple math expression:

2 * 2 + 3

Generally, you would do (2 * 2) + 3 , which equals 7. However, K does not care about primary school rules like PEMDAS. K always read from right to left. parentheses group things to make them execute first. So,

2 * 2 + 3 = 2 * (2 + 3) = 10.

If you want the result to be 7, then you have to use the parentheses shown earlier.

Where most languages have keywords, K has single characters. These characters are called primitives, and these primitives are the lifeblood of K. K has exactly 23 significant symbols that you can create programs with, called primitive verbs. Using these 23 primitive symbols and their multiple overloads, you can create any program you like.

The easiest way to run ngn/k is to go to the online interpreter, available at https://ngn.bitbucket.io/k/. I recommend using the online interpreter until you get confortable with K. If you like running things locally, then feel free to build the interpreter from source: https://codeberg.org/ngn/k/

Let's start with a Hello World:

"Hello, World!"

K will automatically display the result of every line in your program as K data. This means that you can safely paste the result into your program, and use it later.

To actually display a string, you can do this:

` 0:"Hello, World!"

In this program, ` 0: is the equivalent of a print statement in any other language. It will print a simple character array given to it with a newline at the end. You can change it to ` 1: if you want to display without a newline.

K is right to left language. This means that it has no precendence rules other than executing everything from right to left. So, Hello World is read first, then 0:, then the backtick.

Let us take an example:

4 * 5 + 9

Based on primary school math rules, this evaluates to 29. However, K's rules are much simpler: just read everything directly from right to left. For a more visual understanding, here are the steps:

4 * 5 + 9
    |---|

4 * 14
|----|

56

The other major part of our Hello world program is the character array, which brings us to the lego bricks of the array-based language: the arrays.

The things that you can put in an array are called Nouns. These can be:

  • Numbers, which can be simple integers or floating point numbers.
  • Characters, which are enclosed in double quotes
  • Symbols, which start with a backtick, and can start with a . character or any alphabet, followed by any alphanumberic characters + ..
  • Other Arrays
  • Functions and dictionaries, which we will discuss later.

The default way of defining multi-element arrays is as follows:

(<noun1>; <noun2>; <noun3>)

You can also use newlines instead of semicolons to separate array elements.

A single element array of any type can be defined with enlist, which is ,.

,1
,"a"
,`symbol

Character arrays can be made by putting characters in double quotes, as you saw in the Hello World example.

"Hello, world!"
"This is a K string"

You can escape a double quote inside a character array with a backlash. K supports C style escape sequences in its strings, and you can refer to this table for the basic escapes:

Escape Character
\\ \
\n newline
\" "
\t tab

Arrays of symbols are defined by simply listing the symbols together, with no spaces between them.

`a`b`c
`symbol`list

A list of numbers separated by spaces is a numeric array. All of the following are numeric arrays:

45 56 67
420 69
1 2 3

One special thing about K's arrays is that they allow conforming operations. Conforming can be illsustrated by the following operations:

1 2 3 + 4 5 6

gives 5 7 9. Conforming always "penetrates" to the deepest non-array items in an array, so these:

1 + 2 3 4 -> 3 4 5
1 2 3 4 + (1;2 3;4;(5;6 7)) -> (2;4 5;7;(9;10 11))

Are also valid K expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment