Frustrations of Wikitext from a Software Dev

Recursive macros work perfectly fine … One of the famous examples for recursive code is the Fibonacci series.

So the spec is: Print all Fibonacci numbers below 1000.

The code looks as follows. I call it “macronacci” in short “m” since it does not force us to start with 0, 1, 1, 2 … It allows to start with any number n1 and applies the same rules as the Fibonacci series does.

It also has a sep-parameter to define the separator between the numbers. This comes in handy if we want to make the list more readable. eg. linebreaks instead of spaces

\define m(n1:0 n2 sep:" ")
<<__n1__>><<__sep__>>
<$let n1={{{ [<__n1__>match[0]then[1]else<__n1__>] }}}>
  <$let n1={{{ [<n1>add<__n2__>] }}}>
    <$list filter="[<n1>subtract[1000]sign[]match[-1]]">
      <$macrocall $name=m n1=<<n1>> n2=<<__n1__>> sep=<<__sep__>> />
    </$list>
  </$let>
<$let>
\end

<<m>>

The output is

This works perfectly fine. The $macrocall widget is used to call the “m” macro recursively.

Instead of only calculating the numbers with the line <$let n1={{{ [<n1>add<__n2__>] }}}> we could also calculate the quotient for n1/n2 which works by replacing the old formula with the following lines. The quotient should converge against the “golden ratio” which is 1.618033

  <$let n1={{{ [<n1>add<__n2__>] }}} quotient={{{ [<__n1__>divide<__n2__>precision[6]] }}}>
| <<quotient>><br>

As I wrote TW is all about creating lists to “draw” content.

BTW we use recursion often eg: The table of content and the tree macros use recursive code.

2 Likes