K is like APL with fewer built-ins. The syntax is ASCII-only, and most built-in functions are heavily overloaded single bytes of ASCII punctuation.
There are several implementations with subtly varying behavior. code.golf uses ngn/k, which is documented with the reference card found under "help" at the online interpreter. If you're new to K (and J, APL, etc...), this style of documentation might not be very helpful, but other learning material is available:
- Razetime has put together an extensive tutorial which introduces the language in multiple chapters, and then spends some time on solving larger tasks (including a Sudoku solver!).
- If you're looking for something in between the ref-card and a full blown tutorial, Stefan Kruger's kbook might be a good a fit.
- Another implementation of a similar K version is oK which comes with a more extensive manual
Example
Let's look at a program that outputs all of the input arguments with their length appended to them: `0:{x,$#x}'x
(Try it on United States: it outputs Nebraska8
Rhode Island12
…)
`0:{x,$#x}'x
x Get the list of inputs.
{ }' Apply some lambda function to each input.
({} makes a lambda, and the adverb ' means each.)
x x Inside a monad lambda, "x" refers to the single argument.
# Monad # means "length"
$ Monad $ means "convert to string"
, Dyad , means "concatenate"
`0: Output the list of strings line by line.
0: is a dyadic writing-lines-to-a-file verb,
and the left argument ` (the empty symbol) means "write to STDOUT".
You can see that K is essentially read and written "right-to-left", much like J and APL. x,$#x
means (x,($(#x)))
. There's no operator precedence.
Golf tips
\
preceded by a space is "trace" (an identity function for debugging that prints its argument to STDOUT). This can be shorter than using `0:
. Trace however passes its argument through the K formatter before printing, so this may only be useful for numbers.
$[x;T;F]
can become x{T}/F
when x
is a non-negative integer, or (F;T)x
if x
is 0/1 and lazy evaluation is not needed.
x
in the global scope is a list of the command line args.
- Folds can take an optional left argument to use as a seed value. This can often save a byte:
1-+/x
→ 1-/x
, "1.",,/$x
→ "1.",/$x
, ...
- Calling primitives with brackets can sometimes save a byte on parentheses:
((A)+B),C
-> +[A;B],C
(assume A, B and C are longer expressions).
- Strings are sequences of (signed) bytes and can be used in place of a list of small integers in many cases.
- Each type has its own null value which indicates a missing value, for example not found in the result of find, or out-of-bounds indexing:
Type |
Null |
Meaning |
int |
0N |
$-2^{63}$ |
float |
0n |
NaN |
char |
" " |
space |
function |
:: |
identity function |
symbols |
` |
empty symbol |
Base Conversion
These come up a lot, almost in every other problem. A few ideas can be found on the APL tips and J tips page on StackExchange, so here is a quick translation table:
|
K |
J |
APL |
Convert from base |
/ |
#. |
⊥ |
Convert to base |
\ |
#: or #.inv |
⊤ or ⊥⍣¯1 |
n\
combined with strings is very effective at compressing sequences of integers. Example:
28+,/4\">>/" / days in a month (the string is `c$62 11 62 47)
0/x
gets the last value of x
. Compared to *|x
, it can be combined with other adverbs, e.g. 0/'
(last of each), and it returns 0
instead of 0N
for an empty x
.
n/"..."
is a good starting point for a string hash.
External resources