Pancode - Commands - Control Flow

The following commands perform routine control-flow techniques, such as conditionals and primitive loops. Note that commands which operate on lists, such as folds or for-each commands, are listed on the List Operations page.

If (i)

Takes three arguments. The first argument can be any value, but the latter two must be functions. If the first argument is truthy, then the second argument is called. Otherwise, the third argument is called.

Example:

1 [ "true" ] [ "false" ] i « Result: "true" »
0 [ "true" ] [ "false" ] i « Result: "false" »

While (w)

Takes two arguments, both of which must be functions. First, this command calls the first function (the condition). If the first function returns truthy, then this command pops the truthy value and then calls the second function and then repeats the process. If the first function returns falsy at any point, this command stops running, pops the falsy value, and returns immediately.

Example:

« The following prints the numbers from 0 to 9 »
0 [ : 10 < ] [ : . 1 + ] w

Abbreviated While (W)

Similar to While, but the condition and body are combined into one function. Specifically, Abbreviated While takes one argument, which must be a function. That function is called. If it returns a truthy value, then that value is popped and the process repeats. If the function returns a falsy value, then that value is popped and this command returns.

Example:

« The following prints the numbers from 0 to 9 »
0 [ : . 1 + : 10 < ] W

Repeat ()

Takes two arguments: a number and a function. The function is called N times, where N is the provided number. The function will be called with arguments from 0 to N-1, inclusive.

Example:

« The following prints the numbers from 0 to 9 »
10 `. ⍳

Note that if you wish to construct a list indexed by the first N natural numbers, it is simple to do so using Repeat by wrapping it in curly braces. For example

{10ī⍳} « Result: {0 1 2 3 4 5 6 7 8 9} »

Accumulate ()

Takes three arguments: an initial value, a number, and a function. Similar to Repeat, the function will be called N times, where N is the provided number. When the function is called, it will be given two values: the accumulator and the index. The index acts the same as in Repeat (i.e. it will be the numbers from 0 to N-1, inclusive). The accumulator starts off as the initial value and will be set at each iteration to the return value of the last iteration. An eager list of all of the accumulators (including the initial value) is returned.

If the argument N is equal to ∞, then the returned list shall be lazy and infinite in length.

Example:

« The first 7 factorials »
1 6 [1+×] ⍸
« Lazy list of all factorials »
1 ∞ [1+×] ⍸

Call ($)

Pops a single value, which must be a function, and calls it once.

Eval (📝)

Pops a single value off the stack and evaluates it, as though it had appeared literally in the source code. Note that this is different than calling a function ($). If given a function as argument, Eval (📝) will push the function back onto the stack, as function literals evaluate to themselves. If given a symbol or slip, the command or sequence of commands present will be evaluated.

Reflect (🪞)

Performs reflection to convert a function or slip into an array, or vice versa. Pops one value off the stack. If it's a function or a slip, returns an array containing the sequence of values inside the function or slip.

If Reflect is given an array, it creates a slip containing the given values and returns it. With a prime modifier, Reflect will instead return a function containing the given values. The prime modifier has no effect if Reflect is given a slip or function as input.

Note that the Pancode interpreter is permitted to optimize the code inside of a slip or a function. But it will never modify the sequence of values in an array. That is, while the Reflect command is, in spirit, its own inverse, it is not always the case that 🪞🪞 will produce identical code to its input.

Examples:

'「1 2 3」 🪞 « Result: {1 2 3} »
[1 2 3] 🪞 « Result: {1 2 3} »
{1 2 3} 🪞 « Result: '「1 2 3」 »
{1 2 3} 🪞′ « Result: [1 2 3] »

Normalize Boolean (🜀)

Pops one argument. Pushes zero if the argument is zero, or one otherwise.

Logical Conjunction ()

Takes two arguments and returns true if both are truthy, or false otherwise. Subject to binary extension. Returns -1 if given ⚐ or a numerical modifier of zero.

Logical Disjunction ()

Takes two arguments and returns true if either is truthy, or false otherwise. Subject to binary extension. Returns 0 if given ⚐ or a numerical modifier of zero.

Logical Exclusive Or ()

Takes two arguments, normalizes both to Boolean, and returns the exclusive-or of the two. Subject to binary extension. Returns 0 if given ⚐ or a numerical modifier of zero.

Logical Negate ()

Takes one argument. Returns -1 if the argument is falsy, or 0 otherwise.

Documentation Index