My K/Kona page

Here is some information about a very interesting programming language: K, and especially the open source implementation Kona (GitHub).

From Wikipedia K programming language:
K is a [...] array processing language developed by Arthur Whitney and commercialized by Kx Systems. [...] The language, originally developed in 1993, is a variant of APL and contains elements of Scheme. Advocates of the language emphasize its speed, facility in handling arrays and its expressive syntax.

Kona

Here are some links for Kona, an open source implementation of K version 3.2. Some of my Gists about K/Kona:

Kona K code samples

Here are the definitions I have in my definition file. Many are from the pages mentioned above and written by kevinlawler, isawdrones, silentbicycle, danbst, and some by myself, hakank. (Links are to our GitHub pages.)
factorial:{*/1+!:x}
fib1:{(x(|+\)\1 1)[;1]}
fib2:{x{x,+/-2#x}/!2}
fib_rec:{:[x<2;1;_f[x-1]+_f[x-2]]}
maxsubsum:{|/0(0|+)\x}
primes_to_n_sieve:{2_&{:[x@y;x&@[1,-1_ z#(1_ y#1),0;y;:;1];x]}/[x#1;2_!__ceil_sqrt x;x]}
primes_to_n_sieve2:{:[x<4;,2;r,1_&~|/x#'~!:'r: _f[_ _ceil _sqrt x]]}
/ From http://www.nsl.com/k/sieves.k
/ Both version 1 and 2 are fast
primes_to_n:{
              / x 1's at y multiples(y times enum each ceiling x div y) get 0
              s1:{@[x#1;y*!:'-_-x%y;:;0]}
              / primes less than n with sieve s
              p:{[s;n]:[n<4;,2;r,1_&s[n]r:_f[s]@-_-_sqrt n]}
              p[s1;x]}
primes_to_n2:{/ iterate for linear space
              s2:{(x#1){x[y*!-_-(#x)%y]:0;x}/y}
              / primes less than n with sieve s
              p:{[s;n]:[n<4;,2;r,1_&s[n]r:_f[s]@-_-_sqrt n]}
              p[s2;x]}
/ slower version
primes_to_n3:{2_&{&/x!'2_!x}'!:x}
isprime: {(x>1)&&/x!'2_!1+_sqrt x}
/ nextprime:{:[{(x>1)&(&/x!'2_!x)}[x+1];x+1;_f[x+1]]}
nextprime:{~isprime x}{x+1}/
/ n_primes:{(x-1){:[{(x>1)&(&/x!'2_!x)}[x+1];x+1;_f[x+1]]}\2}
n_primes: {(x-1){:[isprime n:x+1;n;_f n]}\2}
/ nth_prime:{*|(x-1){:[{(x>1)&(&/x!'2_!x)}[x+1];x+1;_f[x+1]]}\2}
nth_prime:{*|n_primes x}
/ divisors:{d:&~x!/:!1+_sqrt*x;d,_ x%|d}
divisors:{d:&~x!'!1+_sqrt x;d,_ x%|d}
/ x is not included here
divisors_proper: {d:&~x!'!1+_sqrt x;-1_ d,_ x%|d}
/ factor:{,//{c:0;while[~x!y;x:x%y;c+:1];c#y}[n]'(d@&{(x>1)&(&/x!'2_!x)}'d:,/({&~x!'!1+_(x%2)}n:x),x)}
logn:{(_log x)%_log y}
factor:{
  n:2,3+2*!.5*_sqrt x
  d:n@&~x!'n
  m:d@&~1<+/~d!\:/:d
  p:,/{n:+/0=x!'y^1+!_ logn[x;y];n#y}[x;]'m
  :[_n~p;x;_dv[p,_ x%*/p;1]]}
isprime2:{(x>1) & 1=#factor x}
gcd:{:[~x;y;_f[y;x!y]]}
lcm:{_ x*y%{:[~y;x;_f[y;x!y]]}[x;y]}
totient:{+/1=gcd[x]'!1+x}
squarefree:{:[x=1;1;x>1;~|/1<(+/s=/:s:factor[x]);0]}
mobius:{:[x=1;1;1=squarefree[x];:[1=((#factor[x])!2);1;-1];0]}
pascal:{x{+':0,x,0}\1}
binom:{[n;k] (pascal n)[n;k]}
binom2:{[n;k]i:!(k-1);_*/((n-i)%(i+1))}
digsum:{(+/0$'$:)/ x}
collatz:{(1<){:[x!2;1+3*x;_ x%2]}\x}
leapyear:{(+/~x!'4 100 400)!2}
quicksort:{f:*x@1?#x;:[0=#x;x;,/(_f x@&x<f;x@&x=f;_f x@&x>f)]}
am: {(+/x)%#x}
hmean:%am@%:
gmean:{(*/x)^%#x}
median:{(x@<x)@_(#x)%2}
sv:{+/(_sqr x-am x)%#x}
sd:{_sqrt sv x}
magnitude:{_sqrt+/_sqr x}
sort:{x@<x}

/ ranking of a vector
rankinglt:{<<x}
rankinggt:{>>x}

/ nperm: {{{<:<:y,x}/|x}'+({a-!a:#x}y)_vs(@x),:/x}
nperm: {y@{{<:<:y,x}/|x}'+({a-!a:#x}y)_vs(@x),:/x}
perm:{:[1<x;,/(>:'(x,x)#1,x#0)[;0,'1+_f x-1];,!x]}
perm2:{{x@<x}x{,/x(1!)\'x,'#*x}/,!0}
perm3: {nperm[!*/1+!x;!x]}
permindex:{(a-!a:#x)_sv{+/x<*x}'(!#x)_\:x}
comb:{+a@\:&&/>':a:y _vs!*/x#y}
/ allcomb[9;3]: 3 "series" of 0..8
/ allcomb: {atmp::y # x;{atmp _vs x}' !_ x^y}
/ better version
/ allcomb[4;2] 4 column of all combinations in base 2
allcomb:{+(x#y)_vs!_ y^x}
powset:{x@&:'+2_vs!_2^#x}
/ cartesian product
cart:{(x),/:\:(y)}
/ x first items of y, .e.g ntake[4;!10]
ntake:{(x-#y)_ y}
ispermutationvector:{x~<<x}
overlappinginfixes:{:[y>#x;,x;x@(!y)+/:!(1-y)+#x]}
nonoverlappinginfixes:{(y*!_ceil(#x)%y)_ x}
diagonal1:{x ./:n,'n:!#x}
diagonal2:{x ./:/:f@-1_1_=+/'f:,/n,/:\:n:!#x}
/ all diagonals
diagonals: { ,/((diagonal2 x); (|:' diagonal2 (|+x)))}
table:{b@<b:(x@*:'a),'#:'a:=x}
letter_freq:{?a;#:'=a:,/x}
diff:{,/-':x}
ndiff:{((#y)&x) diff\y}
cycles:{{={x[y]@<x[y]}[x]'!#x}(x\'!#x)}
tonum:{+/(_10^|!#x)*x}
tonum2:{10 _sv x}
inter:{x@&x _lin y}
union:{?x,y}
split:{1_'(&x=y)_ x:y,x}
max_pos:{&x= {(?x)@*>#:'=x} x} 
anagram:{x g@&1<#:'g:={x@<x}'x}
alldiff:{(#x)=#?x}

/ outer product
outer:{[f;a;b]a f/: b}
/ outer product with one self
outerself:{[f;a] a f/: a}

alpha:"abcdefghijklmnopqrstuvwxyz"
rotn:{r:(x!a:_ci(_ic"a")+!26);,/a[&:'(r='/:y)]}
/ rot13:rotn[13]
/ isawdrones' better version
rot13:{a:+65 97+\:2 13#!26;_ci@[!256;a;:;|a]_ic x}

/ Delete leading blanks
dlb: {x@&|\~x=" "}
/ Delete trailing blanks
{|dlb@|x}
/ Delete multiple blanks
{x@&a|1_1!1,a:~x=" "}
/ Left justify
{(+/&\x=" ")!x}
/ Right justify
{(1-(x=" ")_sv 1)!x}
/ Lower Case Letters
lcase: _ci 97+!26
/ Upper Case Letters
ucase: _ci 65+!26
/ To Lower Case
{@[x;p;:;lcase@n@p:&26>n:ucase?/:x]}
/ To Upper Case
{@[x;p;:;ucase@n@p:&26>n:lcase?/:x]}

/ integer partition 
/ from http://www.nsl.com/k/perm.k
/ part:{[v] if[0>i:-1+(v>1)?0;:v]; k:-1+v i; p:i#v;s:+/i _ v; if[~r:s!k;r:!0]; d:(_ s%k)#k; p,d,r}\,:
/    usage 6 part\, 5
part:{[v] if[0>i:-1+(v>1)?0;:v]; k:-1+v i; p:i#v;s:+/i _ v; if[~r:s!k;r:!0]; d:(_ s%k)#k; p,d,r}

/ perfect shuffle for 2*n
perfect_shuffle:{{m:_((#x)%2);,/+(x[!m];x[m+!m])}\(!x*2)}

/ isawdrones' more general version
perfect_shuffle2:{x@<>(#x)#1 0}

/ Gray code
xor: {~x=y}

/ Binary to Gray code
gray:{x[0],xor':x}
gray1:{(x[0],xor[1_ x;-1_ x])}
gray2:{x[0],{:[x[y-1]=1;~x[y];x[y]]}[x]'1+!(#x)-1}

/ Gray code to binary
g2b:xor\
g2b1:*|{gray x}\
g2b2:{c:#x;b:c#0;b[0]:x[0];i:1;do[#x;b[i]:xor[x[i];b[i-1]];i+:1];b}
g2b3:{c:#x;b:c#0;b[0]:x[0];i:1;while[i<c; b[i]:xor[x[i];b[i-1]];i+:1];b}


/ Life, Knuth's approach
/ See E.E. McDonnell: Life: Nasty, Brutish, and Short
life:{m:x;m:m+(1!'m)+-1!'m;m:m+(1!m)+-1!m;m:m+m-x;m _lin\:5 6 7}
/ More nicely formatted:
life2:{m: x
       m: m + (1!'m) + -1!'m
       m: m + (1! m) + -1! m
       m: m + m - x
       m _lin\: 5 6 7
}
/ Run a 100 generations on a 30x30 grid with a random setting
/    "_X" @ 100 life\30 30#(30*30)?0 1


// 8-queens variants
nq1:p@&{a:{(#x)=#?x};i:!#x;a[x[i]+i]&a[x[i]-i]}'(p:perm 8)
nq2:p@&{&/{(#x)=#?x}'(x[i]+i;x[i]-i:!#x)}'(p:perm 8)
nq3:p@&{&/{(#x)=#?x}'(+;-).\:(x[i];i:!#x)}'(p:perm 8)

// SEND+MORE=MONEY, two variants
sendmore1:pp@&{[s;e;n;d;m;o;r;y;a;b]((((1000*s)+(100*e)+(10*n)+d) + ((1000*m)+(100*o)+(10*r)+e)) = ((10000*m)+(1000*o)+(100*n)+(10*e)+y)) & (s>0)&(m>0)&(a<b)}.'(pp:perm 10)

/ another version
/ helper function: f[(1;2;4;0)] returns 1240
f:{+/(_10^|!#x)*x} 
sendmore2:pp@&{[s;e;n;d;m;o;r;y;a;b]((f[(s;e;n;d)]+f[(m;o;r;e)])=f[(m;o;n;e;y)])&(s>0)&(m>0)&(a<b)}.'pp:(perm 10)

// Simulation of Coupon Collector's problem
// here throws of a die until all values has been shown
ccp:table ({#{p:();while[6>#?p;(p,:1?6)];p}[]}'!100)

/ Simulation of Birthday "paradox" (surprise)
/ (here 23 persons)
birthday: {+/x % #x}({(23-(#?23 ? 365))>0}' !10000)

Project Euler

Here are the some entries from Kona's Project Euler Code Golf (where I'm hakank). The first entries are implemented by Kevin and isawdrones and I've included them since they are instructive.

More about K

Here is an unsorted collection of related links about K. Note: some of the code in these pages may not be compatible with Kona. Though they might run under Q's K mode (see below).

Q stuff

Here are some pointers to K's sister language
Q.

Note: When using Q, one can write K code using: And here are the Q related links.
Also see the related:
My APL page
My J page
Back to my homepage.
Created by Hakan Kjellerstrand, hakank@bonetmail.com.