Skip to content

swap

Input Stack:
top: Any
second: Any
Output Stack:
second: Any
top: Any

Exchange the positions of the top two items on the stack. This is one of the most commonly used stack manipulation operations, often needed when operands are in the wrong order for a binary operation.

Parameters

  • top: The item currently at the top of the stack
  • second: The item in the second position that will move to the top

Examples

Basic swap operation:

a,b,:swap
PosInputOutput
0 b a
1 a b

Common use case - getting operands in the correct order for division:

name,errors,:eq,
:sum,
name,requests,:eq,
:sum,
:swap,
:div
  • :over - Copy the second item to the top
  • :dup - Duplicate the top item
  • :rot - Rotate three items (bottom to top)
  • :-rot - Rotate three items (top to bottom)