Mike Austin's Blog

Sunday, March 05, 2006

Very different languages

Here's one way to find prime numbers in Ruby:
def isprime(n)
(2..n-1).map {|x| n % x}.min
end
And here it is translated into the language "K":
isprime: {&/x!/:2_!x}
Here's a step by step breakdown:
!7                       enumerate 7, i.e. 0..6
2_ (!7) drop first 2 elements
: (2_ (!7)) set value of
7 !/ (: (2_ (!7))) vector modulus
&/ (7 !/ (: (2_ (!7)))) vector minimum
If you really want to hurt your eyes, see a larger example:
http://www.kx.com/a/k/examples/xml.k

Interesting for cranking out ad-hoc formulas maybe. But writing programs in it? I don't know about that. But if you want to learn more about it, visit A Shallow Introduction to the K Programming Language