Length 12 Snippet
1_'[3:~2@+]`
This prints the Nth Fibonacci number. New commands here are :
, ~
, and @
. The :
operator creates copies of the top item on the main stack. It's top argument, the 3 in this example, determines the number of copies. So, 0:
is the same as #
, 1:
is a co=op, 2:
makes one additional copy for a total of 2, etc.
The ~
is variable reading. Given a string (which can also be a number) as an argument, it returns the value that was previously stored in that variable. In this example, we are reading from a variable (the number we just made copies of) that we never stored anything in. This gives the empty string, which serves as one argument for the next operator. If we simply replaced ~
with 0
, then we would have to insert a space between the 0
and the 2
.
The @
is stack manipulation. Element only has one operator for rearranging the order of stuff within the stack (as opposed to moving things between stacks, which are '
and "
). The first (bottom) argument of @
is the source index, and the second argument is the destination index. In this program, the arguments are 0 (represented by the empty string) and 2, so this operator consumes those numbers, and then moves the 1st thing in the stack to be the 3rd thing in the stack.
1 push a 1
_' take input then move it to the control stack
[ ] FOR loop
[3: ] make two additional copies of the top number (3 is the total count)
[ ~ ] turn one copy into a zero
[ 2@ ] move from position 0 to position 2, behind the old number
[ +] add the old and newer number
` output the result