Pancode - Commands - List Operations

These are various commands which operate on lists. Note that some of the string commands also work on lists.

Many of these commands extend their arity in a natural way when provided a numerical modifier.

Array End (})

Pops values from the stack until the { array start sentinel is reached, then places all of the popped values into a single (eager) list, returning the list.

Lazy List End (})

Pops one value from the stack, which will serve as the "rest" parameter and must be either a function or the sentinel value ε. Then proceeds to pop values from the stack until the { array start sentinel is reached. Returns a lazy list, with the first N values already forced and equal to the values popped before the sentinel was reached. The "rest" of the lazy list shall be the "rest" parameter.

When the lazy list tries to force a value beyond the available ones, the "rest" parameter will be called. It shall take no arguments and return two values. The second-from-top of the stack is the next value in the list, and the top of the stack is the new "rest" parameter. If the "rest" parameter is ever ε, the list is considered terminated.

Fold (/)

Takes a list and a function. If the list is empty, then this command pushes ⚐ to the stack and calls the function once. If the list has one element, then that argument is returned. If the list has multiple arguments, then the arguments are folded together using the provided function, which should take two arguments.

If you're using a built-in command as your reduction function (e.g. if you're summing a list with `+/), then the case of the empty list involving ⚐ will simply work without any intervention. If you have a custom function and you'd like to handle the empty list case explicitly, the easiest way to do so is with the White Flag Constructor () command.

If given a lazy list, the list will be fully forced. This function will never terminate on an infinite list.

Example:

{1 2 3 4} `+/ « Result: 10 »

Scan (\)

Takes an eager list and a function. Folds the list, similar to /, accumulating each intermediate value along the way. The resulting list of accumulated values shall have the same length as the original list. Note that, if given a list of length zero or one, this function simply returns that list unmodified.

Example:

{1 2 3 4} `+\ « Result: {1 3 6 10} »

Filter ()

Takes an eager list and a mask. The mask can either be a function, a number, or a list of masks. In the first two cases, the mask is repeated into a list of the appropriate length (similar to the scalar extension rules). The length of the mask list must equal that of the input list. At each position of the input list, the corresponding position of the mask list is consulted. If it's a number, it's treated as the constant function returning that number. If it's a function, it's called with the input list element as argument. The absolute value of the result specifies how many times to repeat the value in the resulting list.

With a numerical modifier, the Filter command will nest as deep as the given numerical modifier, so with a modifier of 2, it will nest two list layers deep. A numerical modifier of 20 is treated as infinity.

Examples:

« With a function argument, Filter behaves like the filter function in functional programming languages »
{1 2 3 4 5} [2 | 0 ≡] ⌿ « Result: {2 4} »
« With a list argument, Filter repeats its inputs as specified. »
{1 2 3 4 5} {2 0 3 3 0} ⌿ « Result: {1 1 3 3 3 4 4 4} »

Map (¨)

Takes an eager list and a function. Applies the function to each element of the list, producing a resulting list.

Takes two numerical modifiers. The first numerical modifier specifies the number of lists. If given more than one list, the Map command zips the lists (which must have the same length) together, applying the function to each corresponding tuple of arguments from the respective lists. The second numerical modifier specifies the depth of the mapping operation, in a similar way to Filter's numerical modifier. If the second numerical modifier is 20, it's treated as infinity.

Examples:

{1 2 3 4} [1+] ¨ « Result: {2 3 4 5} »
{1 2 3 4} {10 20 30 40} `+ ¨② « Result: {11 22 33 44} »
{{1 2} {3 4}} [1+] ¨①② « Result: {{2 3} {4 5}} »

Each (ė)

Behaves exactly like Map (¨), except that Each doesn't expect a result from the function it calls and will not accumulate a result list. Each takes the same numerical modifiers as Map.

Nested Query (n)

Takes a list or string and an index. The index should either be a number (in which case it's treated as a singleton list) or a list. The index is traversed in order, taking the nth element of the list or string at each step.

Example:

{{1 2} {3 4}} {1 0} n « Result: 3 »

Select ()

Takes a list or string and an index. The index can either be a number or a list. If it's a number, it's treated as a singleton list. A new list or string (matching the type of the input) is formed, taking the element at each index of the provided index list. Invalid indices are ignored.

Examples:

{100 200 300} {0 2 1} ⊇ « Result: {100 300 200} »
"ABCDEF" {3 5 5 3} ⊇ « Result: "DFFD" »

Grade Up ()

Takes a list and sorts it, according to the natural ordering of its elements. The sort is guaranteed to be stable. The resulting list is of indices in the original list.

With a prime modifier, this command also takes a function, which is used for comparison instead of the standard <.

If given a lazy list, the list will be fully forced before sorting.

Examples:

{"Alpha" "Charlie" "Delta" "Beta"} ⍋ « Result: {0 3 1 2} »
{"Alpha" "Charlie" "Delta" "Beta"} `> ⍋′ « Result: {2 1 3 0} »

Ravel / Flatten ()

Flattens nested lists. By default, this command takes an eager list and flattens one layer of nesting. Given a numerical modifier, this command flattens that many layers of nesting. A numerical modifier of 20 is treated as infinity.

Outer Product ()

Takes two eager lists and a function. Applies the function to every possible pair of elements from the two lists.

Example:

{"a" "b"} {"A" "B" "C"} `⋄ ⊗ « Result: {{"aA" "aB" "aC"} {"bA" "bB" "bC"}} »

If given a numerical modifier, this command pops that many lists and applies the function to n-tuples.

Example:

{"a" "b"} {"A" "B"} {"x" "y"} [⋄⋄] ⊗③ « Result: {{{"aAx" "aAy"} {"aBx" "aBy"}} {{"bAx" "bAy"} {"bBx" "bBy"}}} »

Prepend / Append ()

Takes a value and an eager list and prepends the value to the list. With a numerical modifier N, takes N values and prepends them all to the list. If given a prime modifier, the values are appended to the list instead.

Uncons / Unsnoc ()

The inverse operation to Prepend / Append. With no modifiers, takes a list and pushes the first element of the list and then the rest of the list, with the first element removed.

With a numerical modifier, pops that many elements from the front of the list instead. With a prime modifier, pops from the back instead of the front. If the list is not large enough to support all of the pop operations, then the missing slots will be filled with the sentinel value ϵ.

Example:

{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 'ϵ {} »
{1 2 3} ⛶′ « Result: 3 {1 2} »
{1 2 3} ⛶②′ « Result: 3 2 {1} »
{1 2 3} ⛶③′ « Result: 3 2 1 {} »
{1 2 3} ⛶④′ « Result: 3 2 1 'ϵ {} »

Member ()

Takes an eager list and an element. Returns a list of all of the indices at which the element is found in the list. With a prime modifier, this command instead takes a list and a function and returns all of the elements for which the function returns truthy.

Length (#)

Returns the length of its list argument. If given a numerical modifier, it will count elements of nested lists (up to the given depth) as well. A numerical modifier of 20 is treated as infinity.

Hangs on infinite lists.

Bounds Check ()

Takes a list and a number. Returns whether or not that number is a valid index into the list. A negative number is considered to index from the back of the list.

This command should be used instead of an explicit length check if there's a possibility that the list is infinite. Bounds Check will not hang on infinite lists; it will merely force the list to enough elements to prove that the input is in bounds.

Empty (🗋)

Returns whether or not its list argument is an empty list. If given a prime modifier, this command flattens the list completely before checking whether or not its empty. The following demonstrates the difference between the two variants.

{{} {{}}} 🗋  « Result:  0 (List is not empty) »
{{} {{}}} 🗋′ « Result: -1 (After flattening, list is empty) »

List Constructor ()

By default, this command takes a single argument and wraps it in a list. If given a numerical modifier N, this command pops N values off the stack and places them into a list, starting with the bottommost value.

Take (Left) ()

Takes two arguments: a list and a number. Returns the first N elements of the list, where N is the absolute value of the provided number. If the list is shorter than N, then the original list is returned.

Always returns an eager list, even if given a lazy list as input.

Take (Right) ()

Takes two arguments: a list and a number. Returns the last N elements of the list, where N is the absolute value of the provided number. If the list is shorter than N, then the original list is returned.

Always returns an eager list. Forces the entire input list before returning. This function will always hang on infinite lists.

Drop (Left) ()

Takes two arguments: a list and a number. Returns the list, with the first N elements removed, where N is the absolute value of the provided number. If the list is shorter than N, then the empty list is returned.

Returns a lazy list if given a lazy list.

Drop (Right) ()

Takes two arguments: a list and a number. Returns the list, with the last N elements removed, where N is the absolute value of the provided number. If the list is shorter than N, then the empty list is returned.

Returns a lazy list if given a lazy list.

Take While (Left) ()

Takes two arguments: an eager list and a unary function. Calls the function for each element of the list, starting at the front, until a falsy value is returned. Then the longest prefix of the list for which the function returned true is returned.

Example:

{1 2 3 2 1} [3 <] ◂ « Result: {1 2} »

Take While (Right) ()

Takes two arguments: an eager list and a unary function. Calls the function for each element of the list, starting at the back, until a falsy value is returned. Then the longest suffix of the list for which the function returned true is returned.

Example:

{1 2 3 2 1} [3 <] ▸ « Result: {2 1} »

Drop While (Left) ()

Takes two arguments: an eager list and a unary function. Calls the function for each element of the list, starting at the front, until a falsy value is returned. Then the rest of the list, omitting the prefix for which the function returned true, is returned.

Example:

{1 2 3 2 1} [3 <] ◄ « Result: {3 2 1} »

Drop While (Right) ()

Takes two arguments: an eager list and a unary function. Calls the function for each element of the list, starting at the back, until a falsy value is returned. Then the rest of the list, omitting the suffix for which the function returned true, is returned.

Example:

{1 2 3 2 1} [3 <] ► « Result: {1 2 3} »

Reverse (ɹ)

Takes a single argument, which must be either a string or a list, and returns the argument with its characters or elements, respectively, reversed.

If given a lazy list, forces all elements and returns a (reversed) eager list.

Reshape ()

Takes two arguments: an eager list and a shape. The shape should either be a list or a number. If the shape is a number, it is treated as a single-element list. First, this command flattens the list completely. Then a new list is constructed, where each element of the shape specifies the length of respective sublists. For instance, if the shape is {2 3}, then the resulting list will have two elements, and each of those elements will be a list containing three elements.

The actual elements of the resulting list are drawn from the original argument list, starting at the beginning. If there are too many elements, the later elements of the argument are ignored. If there are too few elements, the elements are repeated cyclically as needed, starting back at the beginning. It is an error to pass an empty list as the first argument to this command.

If given a numerical modifier, that modifier specifies the depth by which to flatten the list before reshaping, using the same rules as the Ravel command. Note that the default numerical modifier to Reshape is 20, which is treated as infinity.

Example:

{1 2 3 4} {2 2} ⍴ « Result: {{1 2} {3 4}} »
{1 2 3} {2 2} ⍴ « Result: {{1 2} {3 1}} »

Lazy List Check (🦥)

Takes one argument. Returns true if the argument is a lazy list. Returns false on non-lazy lists and on arguments which are not lists.

Note that a fully-forced lazy list is still lazy. To convert a lazy list to an eager one, use the Eager-ify (🐇) command.

Eager-ify (🐇)

Converts a list into an eager list, forcing all elements.

Lazy-ify (🐢)

Converts a list into a lazy list. If the list was an eager list, then it will be converted into a fully-forced lazy list. If the list was a lazy list, it is returned unmodified.

First ()

Takes a list and returns its first element.

Second (¹)

Takes a list and returns its second element.

Third (²)

Takes a list and returns its third element.

Fourth (³)

Takes a list and returns its fourth element.

Fifth ()

Takes a list and returns its fifth element.

Sixth ()

Takes a list and returns its sixth element.

Seventh ()

Takes a list and returns its seventh element.

Eighth ()

Takes a list and returns its eighth element.

Ninth ()

Takes a list and returns its ninth element.

Tenth ()

Takes a list and returns its tenth element.

Last ()

Takes a list and returns its last element.

Second Last ()

Takes a list and returns its second-last element.

Third Last ()

Takes a list and returns its third–last element.

Fourth Last ()

Takes a list and returns its fourth–last element.

Fifth Last ()

Takes a list and returns its fifth–last element.

Sixth Last ()

Takes a list and returns its sixth–last element.

Seventh Last ()

Takes a list and returns its seventh–last element.

Eighth Last ()

Takes a list and returns its eighth–last element.

Ninth Last ()

Takes a list and returns its ninth–last element.

Documentation Index