Pancode - Commands - Stack Combinators

The following commands perform higher-order operations on the stack than the more primitive Stack Shuffling commands and should generally be preferred for most nontrivial computations. Many of these commands take a numerical modifier to specify how deep within the stack to operate.

Dip (D)

Pops two arguments: a value and a function. With the two arguments popped, calls the function, then pushes the value back onto the top of the stack, effectively preserving the top element of the stack. If given a numerical modifier, the modifier specifies how many values to preserve.

Example:

1 2 3 4 [100 ×] D « Result: 1 2 300 4 »
1 2 3 4 [100 ×] D② « Result: 1 200 3 4 »

Keep (K)

Pops one function from the stack and peeks at the next value on the stack. Then this command calls the function, then it pushes the value it peeked back on top of the stack, effectively preserving the top element of the stack while still allowing the function to consume it. If given a numerical modifier, the modifier specifies how many values to preserve.

Example:

10 20 [+] K② « Result: 30 10 20 »

Spread ()

By default, Spread pops four arguments off the stack: two arguments and two functions. Then it calls the first function with the first argument and the second function with the second argument, effectively zipping together the arguments and the functions provided.

Spread can be provided two numerical modifiers: A and F, where A defaults to 1 and F defaults to 2. The modifier A specifies the number of arguments each function will be given, and the modifier F specifies how many functions will be provided.

In the following examples, assume a, b, c, and d are arbitrary values and f, g, and h are arbitrary functions.

a b f g ⇉ « Calls f(a) then g(b) »
a b f g ⇉①② « Calls f(a) then g(b) »
a b c f g h ⇉①③ « Calls f(a) then g(b) then h(c) »
a b c d f g ⇉②② « Calls f(a, b) then g(c, d) »

Cross ()

Cross is a very general command, which can be manipulated by its three numerical modifiers.

By default, Cross pops four arguments off the stack: two values a and b and two functions f and g. Then it will call, in order, f(a), f(b), g(a), then g(b).

Many aspects of this command can be specialized. Cross takes three numerical modifiers. The first, A, defaults to 1; the second, B, defaults to 2, and the third, F, defaults to 2. A denotes the number of arguments to pass to each call, B denotes the number of groups of arguments (where each group has size A), and F denotes the number of functions.

This is best demonstrated with several examples. In what follows, assume a, b, c, and d are arbitrary values and f, g, and h are arbitrary functions.

a b f g ⤨ « Calls f(a), f(b), g(a), g(b) »
a b f g ⤨①②② « Calls f(a), f(b), g(a), g(b) »
a b c f ⤨①③① « Calls f(a), f(b), f(c) »
a b c d f ⤨②②① « Calls f(a, b), f(c, d) »
a f g h ⤨①①③ « Calls f(a), g(a), h(a) »

Cross is one of the most complex commands available in Pancode. As such, two specialized forms of the command are available: Apply and Cleave. Consider using one of these simpler commands in preference to Cross if it suits your needs equally well.

Apply ()

Apply is Cross but with the third numerical modifier (F) set to 1 automatically. Apply takes two numerical modifiers, A and B, and behaves otherwise identically to Cross.

Cleave ()

Cleave is Cross but with the second numerical modifier (B) set to 1 automatically. Cleave takes two numerical modifiers, A and F, and behaves otherwise identically to Cross.

Documentation Index