Skip to content

Commit 801b61b

Browse files
committed
document recur
1 parent 29c87e3 commit 801b61b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ This version is not yet released. If you are reading this on the website, then t
4949
- [`un °`](https://uiua.org/docs/un)[`&cd`](https://uiua.org/docs/&cd) will output the current working directory
5050
- [`under ⍜`](https://uiua.org/docs/under)[`&cd`](https://uiua.org/docs/&cd) will return to the original directory afterward
5151
- Add experimental [`reciprocal ⨪`](https://uiua.org/docs/reciprocal) function, which computes the multiplicative inverse AKA reciprocal of a number
52+
- Add experimental [`recur`](https://uiua.org/docs/recur) modifier, which generalizes recursive algorithms
53+
- This would replace existing recursive functions
5254
- Make subscripted [`stack ?`](https://uiua.org/docs/stack) merge adjacent non-subscripted [`stack ?`](https://uiua.org/docs/stack) chains
5355
- Implement [`under ⍜`](https://uiua.org/docs/under)[`regex`](https://uiua.org/docs/regex) for replacing using regex (called `gsub` in some other languages)
5456
### Interpreter

parser/src/defs.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3062,6 +3062,35 @@ primitive!(
30623062
/// [path] is designed to be maximally flexible, so it can be used with graphs or grids or any other structure.
30633063
((2)[2], Path, Algorithm, "path"),
30643064
/// Execute a recursive or tree algorithm
3065+
///
3066+
/// Takes three functions.
3067+
/// The first function checks for a base case.
3068+
/// The second function gets a node's children.
3069+
/// The third function combines parent and child nodes.
3070+
///
3071+
/// If the first function returns a boolean, it determines whether a given node is a leaf node. The second function will only be called on non-leaf nodes.
3072+
/// The third function is passed the results of all a node's children. If it takes at least 2 arguments, it will also be passed the parent node on top.
3073+
///
3074+
/// Many of the examples here can be better expressed using array operations, and are merely demonstrative.
3075+
///
3076+
/// We can express a simple recursive factorial function like so.
3077+
/// ex: Fact ← recur(<2|-1|×)
3078+
/// : Fact 5
3079+
/// : Fact 7
3080+
/// The [less than]`2` determines when to cease recursion. The [subtract]`1` is the next recursive call, the "child node". The [multiply] gets called on a node and the result of its child.
3081+
///
3082+
/// We can express the classic recursive fibanacci function in a similar way. In this example, the second function returns two children. The third function ignores the parent node and simply adds the results of the children.
3083+
/// ex: Fib ← recur(<2|⊃[-1|-2]|/+)
3084+
/// : Fib 10
3085+
/// Because a boolean result from the first function returns a node as its own result, that example interprets the 0th fibonacci number to be `0``.
3086+
/// If we instead want the 0th fibonacci to be `1`, we can return a list of 0 or 1 items from the first function instead. A 1-item list is interpreted as a leaf node, with that item as the result.
3087+
/// ex: Fib ← recur(▽⊙1<2|⊃[-1|-2]|/+)
3088+
/// : Fib 10
3089+
///
3090+
/// The results of a node's children will be passed to the third function as an array. The creation of this array will fail if the results of the children have incompatible shapes. There is an acception for box lists, which will be [join]ed instead of used as rows. This makes it possible to combine variable-length lists.
3091+
/// One example use case for this is listing all files in all subdirectories.
3092+
/// ex: ListFiles ← recur&fif&fld∘
3093+
/// : ListFiles "."
30653094
([3], Recur, Algorithm, "recur", { experimental: true }),
30663095
/// Calculate the derivative of a mathematical expression
30673096
///

0 commit comments

Comments
 (0)