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!
== Overview == K shares key features with [[APL (programming language)|APL]]. They are both [[Interpreted language|interpreted]], [[Interactivity|interactive]] languages noted for concise and expressive syntax. They have simple rules of precedence based on right to left evaluation. The languages contain a rich set of primitive functions designed for processing arrays. These primitive functions include mathematical operations that work on arrays as whole data objects, and array operations, such as sorting or reversing the order of an array. In addition, the language contains special operators that combine with primitive functions to perform types of iteration and recursion. As a result, complex and extended transformations of a dataset can be expressed as a chain of sub-expressions, with each link performing a segment of the calculation and passing the results to the next link in the chain. Like APL, the primitive functions and operators are represented by single or double characters; however, unlike APL, K restricts itself to the [[ASCII character set]] (as does another APL variant, [[J (programming language)|J]]). To allow for this, the set of primitive functions for K is smaller and heavily [[Function overloading|overloaded]], with each of the ASCII symbols representing two or more distinct functions or operations. In a given expression, the actual function referenced is determined by the context. As a result, K expressions can be opaque and difficult to parse for humans. For example, in the following contrived expression the [[exclamation point]] <code>!</code> refers to three distinct functions: <pre>2!!7!4</pre> Reading from right to left the first <code>!</code> is modulo division that is performed on 7 and 4 resulting in 3. The next <code>!</code> is enumeration and lists the integers less than 3, resulting in the list 0 1 2. The final <code>!</code> is rotation where the list on the right is rotated two times to the left producing the final result of 2 0 1. The second core distinction of K is that functions are [[first-class object]]s, a concept borrowed from [[Scheme (programming language)|Scheme]]. [[First-class function]]s can be used in the same contexts where a data value can be used. Functions can be specified as anonymous expressions and used directly with other expressions. Function expressions are specified in K using [[curly bracket]]s. For example, in the following expression a quadratic expression is defined as a function and applied to the values 0 1 2 and 3: <syntaxhighlight lang="k">{(3*x^2)+(2*x)+1}'!4</syntaxhighlight> In K, named functions are simply function expressions stored to a variable in the same way any data value is stored to a variable. <syntaxhighlight lang="k">a:25 f:{(x^2)-1}</syntaxhighlight> Functions can be passed as an argument to another function or returned as a result from a function.
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)