Clip
Clip is a functional language designed for both elegance and brevity. It was inspired by CJam, Lisp, Iota, and Pyth, in that order.
A compiler can be found here. Run using java -jar Clip.jar filename
, or omit the filename to run in a REPL environment.
This page is written fairly technically. Some examples are located here.
Basics
Functions and suppliers
A Clip program consists of one or more named things, which can be suppliers or functions.
A named supplier is indicated by a left-bracket ([) followed by an upper case letter:
[S+3 4
This defines S as the sum of 3 and 4.
A named function is indicated by a left bracket, an upper case letter, and a lower case letter:
[Fx+3x
This defines F as the sum of 3 and the input value. This can then be called using F4 which returns 7.
Each program is automatically prefixed by [M, thus defining M. When a program is run, it prints the value of M:
*8F2[Fx+3x
This returns the product of 8 and the function F applied to 2, where F is the sum of 3 and the input. Thus, the program prints 40.
If M includes a lower-case x, it is initialized to treat x as the input variable:
*8x
is treated as:
[Mx*8x
There are also anonymous functions. These are written as a left-bracket followed by a lower case letter.
+2[d*3d]5
This creates an anonymous "tripling" function, which is applied to 5.
If a bracket is followed by neither a lower-case nor upper-case letter, it is given a placeholder value at the beginning and end:
+2[*3]5
This is the same as above.
Program flow
Each function, when called, evaluates each of its objects in order.
- If the first object is not a function, it is returned. Otherwise, it is a function, which is applied to -
- If the second object is not a function, it is returned. Otherwise, it is a function, which is applied to -
- ... and so on.
- If the second object is not a function, it is returned. Otherwise, it is a function, which is applied to -
If a function hits a backtick (`) or right-bracket (]), it is returned without being evaluated.
An if statement consists of a question mark, an if-clause, a then-clause, a right-bracket, an else-clause, and another right-bracket. If a function hits a question mark, it gets the next result. If it is truthy (not 0 or an empty list), it returns the next value, and then jumps ahead to the second right-bracket. Otherwise, it jumps to the first right-bracket, and continues from there.
?xx]"empty"]
Accepts an input. If it isn't empty, it's outputted. If its is empty, it prints "empty".
Types
There are three main data types, all of which are immutable.
Numbers
Numbers can be of two types: numeric or character. There is rarely a difference, except when printing. Numerics are indicated in code using literals, such as 0, 5, -325, 325.142, -12435.12e4, or 325e-2. Characters literals consist of an apostrophe and another character, such as 'a, '', or '&. A number's ischar is whether it is of the character subtype.
Lists
Lists can be arbitrarily nested. There are no list literals except when consisting of just characters, but they can be approximated using the following technique: Start with the { constant, which means an empty ListBuilder. A ListBuilder is a function which appends each object it meets. Finally, use the backtick to escape the ListBuilder, which has the useful side effect of turning it into an array:
{1 2 3 'a 'b`
Lists of just characters (henceforth known as strings) can be declared as literals using quotation marks. Escape a quotation mark with a period, and a period with two periods. Also, a period followed by an upper case U becomes a period followed by a lower case u, and a period followed by an equals sign becomes a period and a hyphen.
"abc de.".. .U..U .=..="
is actually: abc de". .u.U.-.=
Functions
There are a few types of functions, including named and anonymous ones. Others are pre-initialized to constants (such as + or *), and some are created as the results of other functions.
All functions have exactly one argument. For example, + is a function which takes a single number, and returns another function, which takes another number, and returns the sum. Thus, +1 yields the successor function, and +1 2 gives 3.
Most functions and suppliers are pure; that is, they have no observable side-effects and return the same result for the same input, although there are a few exceptions.
Concepts
Equality
Two numbers are equal if they are mathematically equal, regardless of their ischars. Note that equality of non-integer values is often fraught. For example, =3 *3 /1 3 is false.
Two arrays are equal if they have the same length and corresponding elements are equal.
Two functions are equal if they come from the same declaration. For example, +` equals +`, and named functions equal themselves, as do anonymous functions. However, functions produced from other sources are rarely reflexive. For example, +3` does not equal +3`.
Comparison
Numbers are ordered as expected. Arrays are sorted by element, so "abb" < "abbd" < "abc" < "abca". If corresponding elements are incomparable, an error is triggered.
Boolean values
Numbers are true if they do not equal zero. Arrays are true if they have at least one element, even if that element is false. Functions are always true.
Constants
Constants include named functions and suppliers. There are many default constants as well, as shown in the table below. For simplicity's the sake, the table sometimes refers to a function accepting multiple arguments, although in truth it only accepts one, as explained above. Also, when something is said to be done to a list, the original list is actually left intact; the change is done to a copy of the list which is returned.
Name | Arguments | Explanation | Version |
---|---|---|---|
! | any value | Boolean not. If value is false, returns 1. Otherwise, returns 0. | |
# | number base, number exponent | Returns base to the power of exponent, with the ischar of base. | |
# | number amount, list lis | Returns a list of the first amount values of lis. | |
# | list haystack, any needle | Returns the index of the first value of needle in haystack. | |
$ | any value | Returns value as a string. | |
% | number first, number second | Returns first modulo second, with the ischar of first. | |
% | number amount, list lis | Rotates lis by amount. | |
% | list lis, any value | Returns all indices of lis where the element is value. | v3 |
% | function criterion, list lis | Returns all indices of lis where criterion is true. | v4 |
& | number first, number second | Returns the bitwise-and of first and second, with the ischar of first. | |
& | list first, list second | Returns the intersection of first and second. Each element appears the number of times it is present in both of the original lists. The elements are in the order they are found in first. | |
( | number value | Returns value minus one, with its ischar. | |
( | list value | Returns the first element of value. | |
) | number value | Returns value plus one, with its ischar. | |
) | list value | Returns the last element of value. | |
* | number first, number second | Returns the product of first and second, with the ischar of first. | |
* | number amount, function func | Repeats func amount times. | v4 |
* | number amount, list lis | Returns lis repeat amount times. If amount is not an integer, a portion of lis corresponding to amount's fractional component is appended as well. | |
* | list lis, any value | Places value at the beginning of lis. | |
+ | number first, number second | Returns the sum of first and second, with the ischar of first. | |
+ | number amount, list lis | Returns a list of the last amount values of lis. | |
+ | list lis, number position, any value | Inserts value at position in lis. | v5 |
+ | list first, list second | Returns the concatenation of first and second. | |
, | number first, number second | Returns a list of the numbers from first to second, inclusive, all with the ischar of first. | |
, | list lis, any value | Places value at the end of lis. | |
- | number first, number second | Returns first minus second, with the ischar of first. | v2 only |
- | number value | Returns zero minus value, with the ischar of value. | v3 |
- | list lis, any value | Removes all instances of value from lis. | |
\ | number step, number start, number end, list, lis | Steps through lis with steps of size step, starting from start and ending before end. | v3 |
\ | number step, number start, list, lis | Steps through lis with steps of size step, starting from start. | v3 |
\ | number step, list, lis | Steps through lis with steps of size step, starting from step. | v3 |
\ | list value | Returns a string representing a visual display of value, wherein all characters are wrapped in apostrophes, and all elements are separated with commas. However, if all elements are characters, then the whole list is wrapped in quotation marks.
| |
/ | number first, number second | Returns first divided by second, with the ischar of first. | |
/ | number pieces, list lis | Splits lis into pieces. Any leftover elements will be placed as early as possible. | v3 |
/ | list lis, list delimiter | Breaks apart lis by delimiter, and returns a list containing the sublists. | |
: | number amount, any value | Returns a list containing amount copies of value. | |
: | list of numbers indices, list lis | Returns the elements of lis located at indices. | v3 |
< | number first, number second | Returns 1 if first is less than second, and 0 otherwise. | |
< | list first, list second | Returns 1 if first is less than second, and 0 otherwise. | |
= | any first, any second | Returns 1 if first is equal to second, and 0 otherwise. | v2 - v4 for number,number and list,list; v5 for any,any |
> | list first, list second | Returns first, with elements of second removed. Elements are only removed once per occurrence in second. | |
A | The string "ABCDEFGHIJKLMNOPQRSTUVWXYZ". | ||
B | The number one-half. | v3 | |
C | list value | Breaks apart value by spaces. Equivalent to / value " ". | |
F | list first, list second | Returns a function which maps first to second. When this function is applied to an element in first, the corresponding element in second is returned. | |
G | The number -1. | ||
H | The number -2. | ||
I | any value | Returns value. | |
J | list of lists value | Joins the lists of value, separated with a newline character. | |
K | any first, any second | Returns first. | |
L | An empty list. | ||
M | Any x | Runs the program on input x. | |
N | The newline character. | ||
O | The number 1. | ||
P | The number pi, accurate to at least fifty decimal places. | ||
Q | The next whitespace-separated token from the console. | ||
R | number value | If value is non-negative, returns a list of numbers from 0 until value, including 0 but not value. If value is negative, returns a list of numbers from value until 0, including 0 but not value. In either case, the numbers have the ischar of value. | |
R | list value | Shuffles the list. | |
R | function reduction, list lis, any identity | Reduces lis using reduction and identity. | |
S | The space character. | ||
T | The number 10. | ||
V | list lis, list removals | Removes all appearances of all elements of removals from lis. | |
W | The number 2. | ||
X | The number 16. | ||
Z | The number 0. | ||
^ | number first, number second | Returns the bitwise-xor of first and second, with the ischar of first. | |
^ | list first, list second | Returns the symmetric difference of first and second. Each element appears the difference of the numbers of times it is present in each list. | |
a | number value | Returns the absolute value of value. | |
a | list lis, any find, any replacement | In lis, replaces each find with replacement. | |
a | function criterion, list lis, any replacement | In lis, replaces each element where criterion returns true with replacement. | |
b | number base, number value | Converts value to base, putting the result in a list of numbers. | |
b | number base, list value | Considers value as a number in base, and returns the actual number represented. | |
b | list value, number first, number second | Considers value as a number in base first, and converts it to base second, putting the result in a list of numbers. | |
b | list lis, list search, any replacement | Replaces any instances of elements of search within lis with replacement. | |
c | number value | Returns value as a character. | |
c | list lis, any value | Returns the number of times value is present in lis. | |
c | function first, function second | Returns the composition of first and second. | |
f | number value | Returns a list of the factors of value. | v5 |
f | list value | Flattens value. For each element that is a list, it is recursively replaced with its contents. | |
f | function criterion, list lis | Removes all elements from lis where criterion is false. | |
g | number index, list lis | Returns the index'th element of lis. | |
g | list of numbers lis, list of numbers search, list of numbers replacement | Performs a regular-expression replacement on lis, replacing each search by replacement. | |
h | list lis | Removes the last element from lis. | |
i | number value | Rounds value towards zero. | |
i | list lis | Removes the first element from lis. | |
i | function mapper, list lis | Maps each element of lis, using mapper, a function which accepts one element of lis and an index, ranging from 0 to one less than the length of lis. | v3 |
i | function mapper, function condition, any value | Maps value using mapper until condition returns false. | v5 |
j | list of lists value, list delimiter | Joins the lists of value, separated with the elements of delimiter. | |
j | non-empty list of numbers file, list of numbers contents | Writes contents to filename, and returns its old contents. | v4 |
j | function joiner, list first, list second | Returns a list consisting of joiner applied to respective elements of first and second. The size is the size of first. For example, \j+`{1 2 3`{5 10` results in {6, 12, 8}. | |
k | non-empty list of numbers value | Interprets value as a human readable list, as might be produced by \ . | |
k | list of lists lis | Transposes lis. | |
k | function generator, number size, any seed | Generates a list of size elements, where the first is seed and subsequent elements are the result of generator applied to the preceding element. | v3 |
k | function generator, list seeds, number size | Generates a list of size elements, where the first elements are seeds and subsequent elements are the result of generator applied to the list of all preceding elements. | v3 |
k | function joiner, list first, list second | Returns a list consisting of joiner applied to all pairs of elements of first and second. For example, \k+`{1 2 3`{5 10` results in {6, 11, 7, 12, 8, 13}. | |
k | function joiner, list seeds, function condition | Generates a list of elements, where the first elements are seeds and subsequent elements are the result of generator applied to the list of all preceding elements, as long as condition is true. | v5 |
k | function generator, function condition, any seed | Generates a list of elements, where the first element is seed and subsequent elements are the result of generator applied to the preceding element, as long as condition is true. | v5 |
l | number value, number base | Returns the logarithm of value using base, with the ischar of value. | |
l | list lis | Returns the size of lis. | |
l | function mapper, list lis | Maps each element of lis using mapper, traveling recursively, so all elements of elements of lis will be mapped as well, and so on. | v3 |
m | number first, number second | Returns first minus second, with the ischar of first. | |
m | number size, list lis | Splits lis into pieces of size elements each. Leftover elements will be placed in an extra kust. | v3 |
m | function mapper, number size | Maps the first size integers using mapper. | v5 |
m | function mapper, list lis | Maps each element of lis using mapper. | |
n | number value | Returns value as a numeric. | |
n | non-empty list of numbers value | Interprets value as a string, and reads it as a number. | |
n | list of functions lis, any value | Applies each function in lis to value. | |
o | number value, number precision | Rounds value to precision places past the decimal point. | v5 |
o | non-empty list of numbers file | Returns the contents of file. | |
o | list of functions mappers, list lis | Applies each function in mappers to the corresponding element of lis. The result's size is the size of values. | |
o | function mapper, list lis | Maps each element of lis to a function accepting an argument and applying mapper to that first. For example, \oo-`{1 2 3`{10 20 30` is {9, 18, 27}. In contrast, \om-`{1 2 3`{10 20 30` is {-9, -18, -27}. | |
p | any value | Prints value and returns it. | |
q | list value | Returns a list of pairs, with each pair consisting of an element of a value and the number of occurrences therein. For example, \q"hello world" is {{'l', 3}, {'o', 2}, {'h', 1}, {'w', 1}, {'e', 1}, {'d', 1}, {'r', 1}, {' ', 1}}. | v5 |
r | number value | If value is non-negative, returns a random non-negative integer less than value. If value is negative, returns a random non-positive integer greater than value. | |
r | list lis | Returns a randomly chosen element of lis. | |
r | function reduction, list lis | Reduces lis using reduction. | |
s | number index, list lis, any value | Sets the value of lis at index to be value. | |
s | list lis | Sorts lis from lowest to highest. | |
s | function mapper, list list | Maps lis using mapper, and sorts it from lowest to highest. For example, \sa`{-2 -1 0 1 2` is {0, 1, 1, 2, 2}. | v2 only |
s | function mapper, list list | Sorts lis from lowest to highest using mapper. For example, \sa`{-2 -1 0 1 2` is {0, -1, 1, -2, 2}. | v3 |
t | Returns the number of milliseconds since January 1, 1970, 0:00:00. | ||
u | number start, number end, list lis | Returns a list with all of the elements of lis from start (inclusive) to end (exclusive). | |
u | list lis | Removes duplicate elements from lis. | |
v | number value | Returns the mathematical inverse of value. | v4 |
v | list lis | Reverses the order of lis. | |
v | function binary, any first, any second | Applies binary to second and then to first. | v4 |
w | any value | Returns a list with value as its only element. | |
y | any first, any second | Returns a list of two elements: first and second. Note that this can not be used in the main function if x appears therein. .y can be used instead. | v5 |
{ | An empty ListBuilder. | ||
number first, number second | Returns the bitwise-or of first and second, with the ischar of first. | v4 | |
list first, list second | Returns the union of first and second. Each element appears the most number of times it is present in either of the original lists. The elements are in the order they are found in first, followed by the remainder in second. | v4 | |
~ | number value | Returns the bitwise-not of value. | v4 |
.% | list lis, any value | Returns all locations of lis where the element is value, where each location is represented by a list of indices within the sublists. For example, \.%{'a "cat" "afar" {{'a``` 'a is {{0}, {1, 1}, {2, 0}, {2, 2}, {3, 0, 0}}. | v5 |
.% | function criterion, list lis | Returns all locations of lis where criterion is true, where each location is represented by a list of indices within the sublists. For example, \.%l`{L{LL{L`L{{L```` is {{1}, {1, 2}, {1, 4}, {1, 4, 0}}. | v5 |
.C | number value | Returns the arccosine of value. | v4 |
.D | number value | Considers value as a base-36 digit, and returns the number. | v4 |
.I | non-empty list of numbers site | Returns the contents of site, a page on the Internet. | v5 |
.L | number value | Returns the lowercase value of value. | v4 |
.S | number value | Returns the arcsine of value. | v4 |
.T | number value | Returns the arctangent of value. | v4 |
.U | number value | Returns the uppercase value of value. | v4 |
.c | number value | Returns the cosine of value. | v4 |
.d | number value | Converts value to a base-36 digit. | v4 |
.j | list lis, any value | Places value in between every two elements of lis. | v5 |
.m | list of numbers data | Returns the arithmetic mean of data | v5 |
.s | number value | Returns the sine of value. | v4 |
.s | list of numbers data | Returns the standard deviation of data | v5 |
.t | number value | Returns the tangent of value. | v4 |
.y | any first, any second | Returns a list of two elements: first and second. This is equivalent to y. | v5 |
ee | The mathematical constant e. | v4 | |
er | A random real number at least 0 and less than 1, with 50 decimal digits. | v5 | |
et | number value | The number 1. | v5 |
et | list value | The number 0. | v5 |
et | function value | The number -1. | v5 |
ev | list of numbers program | Undefined behavior. One likely result is to return the value found by running program. | v3 |
ew | number time | Pauses for time milliseconds. | v4 |
ex | Exits the program immediately and completely. | v3 |
Compiling
When a program is compiled, a number of things happen, in the following order:
- All comments are removed. .-Comments look like this-.
- Unicode substitution takes place. For each .u present, the next four digits are interpreted as a hexadecimal character code.
- Strings and character literals are identified.
- Named functions and suppliers are separated.
- Right-braces are converted into right-brackets and backticks ( } -> ]` ).
- Missing right-brackets are placed.
- Number literals are identified.
- The main thing is given the name M and wrapped in brackets. If it contains an x, it is considered a function of x. If it also contains a y, its items are placed in an anonymous function of y. If it also contains a z, its items are placed in an anonymous function of z.
- Each named thing has its anonymous functions and if-statements identified.
- The named things are put into an executable.
Afterwards, M is printed.