GolfScript Built-ins

Below is a detailed description of all built-ins along with examples for all different applications of that built-in.

These "functions" don't really have arguments but they do typically consume a fixed number of items off the stack. Coerce indicates the two arguments are coerced to the same type, order indicates that if the first arg's type priority is less than the second arg's then swap those args.

~

args: 1
Bitwise not for integers.
5~ -> -6
Evaluate for strings and blocks.
"1 2+"~ -> 3
{1 2+}~ -> 3
Dump elements for arrays.
[1 2 3]~ -> 1 2 3


`

args: 1
The inverse of ~, generates a string that if evaluated returns the original. Gets its name from python where `...` has a similar effect.
1` -> "1"
[1 [2] 'asdf']` -> "[1 [2] \"asdf\"]"
"1"` -> "\"1\""
{1}` -> "{1}"


!

args: 1
0 [] "" {} yield 1, everything else 0
1! -> 0
{asdf}! -> 0
""! -> 1


@

args: 3
desc:Rotates the top 3 elements of the stack so that the 3rd down is now on top.
1 2 3 4 @ -> 1 3 4 2


#

args:
Not actually a built in variable, it is part of the syntax ignoring everything until newline.

$

args: 1 or 2
If arg is an integer, copys nth item from top of $tack.
1 2 3 4 5  1$ -> 1 2 3 4 5 4
For arrays (including strings) a $ort is performed.
'asdf'$ -> "adfs"
For blocks, sort by some mapping.
 [5 4 3 1 2]{-1*}$ -> [5 4 3 2 1]


+

args: coerce
Adds two numbers or concatenate
5 7+ -> 12
'asdf'{1234}+ -> {asdf 1234}
[1 2 3][4 5]+ -> [1 2 3 4 5]


-

args: coerce
Note the way - is parsed in the first example.
1 2-3+ -> 1 -1
1 2 -3+ -> 1 -1
1 2- 3+ -> 2
[5 2 5 4 1 1][1 2]- -> [5 5 4]


*

args: order
* can mean many things, the choice of behavior is determined by the type.
Multiplication
 2 4* -> 8
Execute a block a certain number of times, note the order of operands does not matter because these are automatically ordered first.
 2 {2*} 5* -> 64
Array/string repeat
[1 2 3]2* -> [1 2 3 1 2 3]
3'asdf'* -> "asdfasdfasdf"
Join
[1 2 3]','* -> "1,2,3"
[1 2 3][4]* -> [1 4 2 4 3]
'asdf'' '* -> "a s d f"
[1 [2] [3 [4 [5]]]]'-'* -> "1-\002-\003\004\005"
[1 [2] [3 [4 [5]]]][6 7]* -> [1 6 7 2 6 7 3 [4 [5]]]
Fold. Symbol choice for fold comes from ruby golf trick: eval [1,2,3,4,5]*"+".
[1 2 3 4]{+}* -> 10
'asdf'{+}* -> 414


/

args: order
/ can also mean many things based on operand types, usually the opposite of *
Division
7 3 / -> 2
Split around matches with a second array
[1 2 3 4 2 3 5][2 3]/ -> [[1] [4] [5]]
'a s d f'' '/ -> ["a" "s" "d" "f"]
Split into groups of a specified size
[1 2 3 4 5] 2/ -> [[1 2] [3 4] [5]]
Unfold, a slightly complicated function. It is pretty much a while loop except that before each condition test, the top of stack is duplicated, and if that condition check passes the current top of stack is collected into an array.
0 1 {100<} { .@+ } / -> 89 [1 1 2 3 5 8 13 21 34 55 89]
Each. Execute a block over all elements.
[1 2 3]{1+}/ -> 2 3 4


%

args: order
Modulus
7 3 % -> 1
Split array around another array, like /, but remove empty results.
'assdfs' 's'% -> ["a" "df"]
'assdfs' 's'/ -> ["a" "" "df" ""]
Select elements who's index is 0 mod arg, this is essentially to python's array indexing [::number], so like python if it is negative the result will be reversed.
[1 2 3 4 5] 2% -> [1 3 5]
[1 2 3 4 5] -1% -> [5 4 3 2 1]
Map
[1 2 3]{.}% -> [1 1 2 2 3 3]


|

args: coerce
Bitwise/setwise or
5 3 | -> 7


&

args: coerce
Bitwise/setwise and
[1 1 2 2][1 3]& -> [1]


^

args: coerce
Bitwise xor/symmetric set difference
[1 1 2 2][1 3]^ -> [2 3]


{ }

Block creation, note that this is not actually a built in variable, instead it is part of the syntax.

'

Raw string, only \ and ' can be escaped
'\n' -> "\\n"
' \' ' -> " ' "


"

Escaped string
"\n" -> "\n"
"\144" -> "d"


[ ]

You might be surprised to learn that [ ] are not built into the syntax. Instead [ marks the stack size when it is executed, and a ] slices the stack back at where it was marked. Anytime the stack is made smaller where it was marked, the [ is moved appropriately. In this example you might expect the result to be 1 2 [], but during the swap those two values are temporarily removed from the stack, causing the [ to move.
1 2 [\] -> [2 1]


\

args: 2
Swap top two elements of the stack
1 2 3 \ -> 1 3 2


:

args: 1*
Assign the value of the top of the stack to the variable immediately after the :. This is built into the syntax not a built-in variable. Does not consume top of stack.
1:a a -> 1 1
1:0;0 -> 1


;

args: 1
Pop and discard top of stack.
 1 2 3; -> 1 2


<

args: order
For same types, perform less than comparison, pushing 1 for true, 0 for false. Otherwise select elements in an array whose index is less than the integer specified. This is like python's [:n:] usage.
3 4 < -> 1
"asdf" "asdg" < -> 1
[1 2 3] 2 < -> [1 2]
{asdf} -1 < -> {asd}


>

args: order
For same types, perform greater than comparison, pushing 1 for true, 0 for false. Otherwise select elements in an array whose index is greater than or equal to the integer specified. This is like python's [n::] usage. Note: for mixed types order does matter with these types of operations.
3 4 > -> 0
"asdf" "asdg" > -> 0
[1 2 3] 2 > -> [3]
{asdf} -1 > -> {f}


=

args: order
For same types, perform equal to comparison, pushing 1 for true, 0 for false. Otherwise select element in an array whose index is equal to the integer specified. If the index is out of bounds it adds nothing to the stack. This is similar python's [n] usage.
3 4 = -> 0
"asdf" "asdg" = -> 0
[1 2 3] 2 = -> 3
{asdf} -1 = -> 102


,

args: 1 or 2
Create an array of n elements starting at 0.
 10, -> [0 1 2 3 4 5 6 7 8 9]
Get the size of an array
 10,, -> 10
If the argument is a block, take another argument and perform a map, select all original elements in the array whose result was true.
 10,{3%}, -> [1 2 4 5 7 8]


.

args: 1
Duplicate top of stack.
1 2 3. -> 1 2 3 3


?

args: order
Raise an integer to a power.
2 8? -> 256
What is the index of this element? -1 for none existent.
5 [4 3 5 1] ? -> 2
Find. Which element satisfies this condition first?
[1 2 3 4 5 6] {.* 20>} ? -> 5


(

args: 1
De-increment a number.
 5( -> 4
Uncons for arrays. Uncons is stolen from lisp, it takes the first element out of the array and places the remaining array along with the first element on the stack.
[1 2 3]( -> [2 3] 1


)

args: 1
Increment a number.
 5) -> 6
Uncons from the right for arrays.
[1 2 3]) -> [1 2] 3


and or xor

args: 2
Lazy boolean. Implemented as:
{1$if}:and;
{1$\if}:or;
{\!!{!}*}:xor;
5 {1 0/} or -> 5
5 {1 1+} and -> 2
0 [3] xor -> [3]
2 [3] xor -> 0


print

args: 1
Convert to a string and print.

p

args: 1
Convert to a string using ` (inspect) and print followed by variable n. Same deal as ruby. Implemented as:
{`puts}:p;


n

args: 0
Implemented as:
"\n":n;


puts

args: 1
Convert to a string and print followed by variable n. Same deal as ruby. Implemented as:
{print n print}:puts;


rand

args: 1
Push random integer up but not including.
5 rand --> 2


do

args: 1
A do ... while loop. Execute a block, pop value, if this is true then continue.
5{1-..}do -> 4 3 2 1 0 0


while until

args: 2
Execute a condition block, pop a value, if true/false execute body block.
5{.}{1-.}while -> 4 3 2 1 0 0
5{.}{1-.}until -> 5


if

args: 3
Pop condition value (first arg). If true execute first, otherwise execute second.
1 2 3 if -> 2
0 2 {1.} if -> 1 1


abs

args: 1
-2 abs -> 2


zip


args: 1
Transpose array rows with columns.
[[1 2 3][4 5 6][7 8 9]]zip -> [[1 4 7] [2 5 8] [3 6 9]]
['asdf''1234']zip -> ["a1" "s2" "d3" "f4"]


base

args: 2
Unsigned base conversion, second arg is radix.
[1 1 0] 2 base -> 6
6 2 base -> [1 1 0]