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.
- Problem 1:
+/&~&/(!1e3)!/:3 5
(Kevin)
- Problen 2: Known bound:
*+/{x*~x!2}32(|+\)\1 1
Unknown bound(b f/ x) : +/{x@&~x!2}(4e6>+/-2#){x,+/-2#x}/1
(isawdrones)
- Problem 3:
|/d@&&/'2_'f'd:&~(f:{x!'!1+_sqrt x})600851475143
(isawdrones)
- Problem 4:
|/b@&{x~|x}'$b:,/a*/:a:!1000
Faster: |/b@&{x~|x}'$b:?,/a*/:a:100+!900
(isawdrones)
- Problem 5: Euclid’s algorithm:
{x*y%{:[y;_f[y]x!y;x]}[x]y}/1+!20
(isawdrones)
- Problem 6:
_(_sqr+/a)-+/a*a:1+!100
(isawdrones)
- Problem 7:
p:{:[x<4;,2;r,1_&~|/x#'~!:'r: _f[_ _ceil _sqrt x]]}
b:2;while[1e4>#a:p b*:2];a[10000] (isawdrones)
- Problem 8:
|/*/'{x@(!5)+/:!-4+#x}0$',/0:`
(isawdrones)
- Problem 9: Brute force:
*/_*c@&1000=+/'c:b,'(_sqrt+/)'b*b:,/a,/:\:a:!500
(isawdrones)
- Problem 10:
p:{2_&{:[x@y;x&@[1,-1_ z#(1_ y#1),0;y;:;1];x]}/[x#1;2_! __ceil _sqrt x;x]};+/p@_2e6
(Kevin)
- Problem 12: Brute force:
f:{1__ x*(x+1)%2};c:1;{x<500}{#{d:&~x!'!1+_sqrt x;d,_ x%|d}f c+:1}\1;f@c
(hakank)
- Problem 19:
+/{(&/"01"=-2#$_dj x)&6=x!7}'from+!-((from:_(_jd 19010101))-(_jd 20001231))
(hakank)
- Problem 24:
({:[1:'(x,x)#1,x#0)[;0,'1+_f x-1];,!x]}10)[_1e6-1]
(hakank)
- Problem 30:
+/2_&{x=+/(0$'$x)^5}'!7*9^5
(hakank)
- Problem 34:
+/2_&{x=+/{*/1+!x}'0$'$x}'!50000
(hakank)
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).
- Wikipedia: K Programming language
- Rosetta Code: K, Tasks not implemented in K, my (Hakank's) Contributions
- Dennis Shasha: K as a Prototyping Language. (Dennis Shasha has also done research using K: Research Summary)
- A Shallow Introduction to the K Programming Language
- kx.com: K reference card (PDF)
- www.kxcommunity.com
- Wayback machine: K user manual
- Wayback machine: K reference manual (PDF)
- Video: K Screencast 01 - Introducing Kona
- Video: K Screencast 02 - MapReduce in K
- Video: Java vs K
- Arthur Whitney K (Vector)
- Boyko Bantchev (Programming Language Awareness Centre): K
- AttilaVrabecz:K Programming Language, including: K by example (compare with Oleg's comparison with J below)
- langreiter.com: K3-notes
- langreiter.com: Functions/Programs
- www.kx.com
- kx.com: Kx examples
- c2.com: KayLanguage
- Journal Vector, has interesting articles
- StackOverflow: APL versus J versus K
- joelonsoftware.com: Superstar programmers, about Arthur W and K, etc
- kx.com: An Interview with Arthur Whitney (2004)
- ACM Queue: A Conversation with Arthur Whitney (2009)
- User danbst has an Ukrainian blog where he wrote about K: danbst.wordpress.com, K not C, translated to English. The complete blog translated.
- Lambda-the-ultimate: Rich resource site for the programming language "K"
- Stevan Apter: No Stinking Loops
- Stevan Apter articles in Vector
- Stevan Apter: K: Remarks on Style (PDF)
- Stevan Apter's K programs: http://nsl.com/k/
- Interview by Stevan Apter: A Conversation with Manfred von Thun
- How to do Interprocess Communication in k3
- comp.lang.apl: APL, J, or K?
- StackOverflow, tagged [k]
- StackOverflow, "Program memory footprint for different interpreters/compilers"
- news.ycombinator.com: About APL, J, and K
- Comments on "Why You Should Learn "Weird" Languages (alexthornton.net) "
- ycombinator.com: What's so cool about APL
- Wikipedia: Eugene_McDonnell (another K guru)
- Eugene McDonnell's K idioms: kidioms.doc
- Eugene McDonnell APL/K papers (not all are available yet)
- aiju.de: Different functions in K
- kx.com /a/k/examples
- kx.com /technical/contribs/
- kx.com Documentation/
- Slashdot What about K? (from 2002)
- Bob Armstrong: K/Cosy, K_CoSy (idioms), searchfns, and Facts, Constants & Conversions
- Dick Bowman A Quick look at K
- Oleg's J page: J vs K by Example (as PDF)
- Milan's Website - a site about KDB+ and K4
- Jan Karman Ganuenta - Actuarial & Financial Information Systems (see expecially
samples, K
)
- thesweeheng’s weblog: Category k (not very active anymore)
- chl's k page at delicious.com
- Inspiration: Dyalog APL dfns
- Arthur Withney's kParc project. See discussion at reddit/apljk
- Discussion about differences between APL/J/K in GNU APL 1.0
K programs in some other systems
Q stuff
Here are some pointers to K's sister language Q.
Note: When using Q, one can write K code using:
-
k) +/!100
-
\
mysum:{+/!x}
mysum[10]
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.