Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
K (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Examples == K is an interpreted language where every statement is evaluated and its results displayed immediately. Literal expressions such as strings evaluate to themselves. Consequently, the [["Hello, World!" program|Hello world]]-program is trivial: <pre> "Hello world!" </pre> The following expression sorts a list of strings by their lengths: <syntaxhighlight lang="k"> x@>#:'x </syntaxhighlight> The expression is evaluated from right to left as follows: # #:'x returns the length of each word in the list x. # > returns the indices that would sort a list of values in descending order. # @ uses the integer values on the right to index into the original list of strings. A function to determine if a number is prime can be written as: <syntaxhighlight lang="k"> {&/x!/:2_!x} </syntaxhighlight> The function is evaluated from right to left: # !x enumerate the positive integers less than x. # 2_ drops the first two elements of the enumeration (0 and 1). # x!/: performs modulo division between the original integer and each value in the truncated list. # &/ find the minimum value of the list of modulo result. If x is not prime then one of the values returned by the modulo operation will be 0 and consequently the minimal value of the list. If x is prime then the minimal value will be 1, because x mod 2 is 1 for any prime greater than 2. The function below can be used to list all of the prime numbers between 1 and R with: <syntaxhighlight lang="k"> 2_&{&/x!/:2_!x}'!R </syntaxhighlight> The expression is evaluated from right to left # !R enumerate the integers less than R. # ' apply each value of the enumeration to the prime number function on the left. This will return a list of 0's and 1's. # & return the indices of the list where the value is 1. # 2_ drop the first two elements of the enumeration (0 and 1)
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)