all repos — hex @ cebe9e6368b0d18e5ca7d2111f31634eeb37715b

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

Fixed tutorial
Fabio Cevasco h3rald@h3rald.com
Sat, 25 Jan 2025 22:07:08 +0100
commit

cebe9e6368b0d18e5ca7d2111f31634eeb37715b

parent

7aa4e288667ecb264cf370bf7f47748a5062069f

1 files changed, 10 insertions(+), 10 deletions(-)

jump to
M web/contents/learn.htmlweb/contents/learn.html

@@ -157,22 +157,22 @@ <p>...What? You don't even know how to implement a loop or a condition? But it's all the same! It's always values pushed and popped from the stack using symbols!</p>

<p>Alright, let's do something actually useful. I know: let's implement a new operator to implement the <em>factorial</em> of an integer! You never know when you'll need a factorial these days.</p> <p>Here goes:</p> <pre><code>( - $"_n"$$ $:$$ - ($:_n$$ $0x0$$ $:&lt;=$$) + $"_fact_n"$$ $:$$ + ($:_fact_n$$ $0x0$$ $:&lt;=$$) ($0x1$$) - ($:_n$$ $:_n$$ $0x1$$ $:-$$ $:factorial$$ $:*$$) + ($:_fact_n$$ $:_fact_n$$ $0x1$$ $:-$$ $:fact$$ $:*$$) $:if$$ - $"_n"$$ $:#$$ -) $"_factorial"$$ $:::$$ + $"_fact_n"$$ $:#$$ +) $"fact"$$ $:::$$ -$0x5$$ $:factorial$$ $:dec$$ $:puts$$ $; Prints 120 $$ +$0x5$$ $:fact$$ $:dec$$ $:puts$$ $; Prints 120 $$ </code></pre> -<p>Woah! That was a mouthful, wasn't it? Before breaking it down, look at the very end of the program: see that {{sym-::}}? That's the symbol to store <em>operator</em> symbols in The Registry. Operator symbols are defined using a quotation, but unlike ordinary quotations (stored using {{sym-:}}), they will be <em>immediately dequoted</em> when pushed on the stack. In other words, our $:factorial$$ operator symbols will behave exactly like one of the built-in native symbol.</p> +<p>Woah! That was a mouthful, wasn't it? Before breaking it down, look at the very end of the program: see that {{sym-::}}? That's the symbol to store <em>operator</em> symbols in The Registry. Operator symbols are defined using a quotation, but unlike ordinary quotations (stored using {{sym-:}}), they will be <em>immediately dequoted</em> when pushed on the stack. In other words, our $:fact$$ operator symbols will behave exactly like one of the built-in native symbol.</p> <p>Let's see what is happening inside the quotation:</p> <ul> -<li>First, we are storing a symbol &quot;_n&quot; in the registry. Wait, but there's no value? Correct, the value will be provided when the symbol is used, so like $0x5$$ $:factorial$$. It's like saying that we are <em>expecting</em> a value on the stack (and here we are assuming it's an integer, but that's ok for this example).</li> -<li>Then three (!) quotations and the symbol {{sym-if}} will be pushed on the stack. Yep, you got that right: that's a good old if clause. The first quotation is the condition to be checked, then the <em>then</em> quotation, and finally the <em>else</em> quotation. Note how we can recursively call the $:factorial$$ operator that we are just defining... mind-blowing, I am sure.</li> -<li>Finally, we need to free the temporary symbol $:_n$$ using {{sym-#}}. It is always good practice to do so, otherwise The Registry will be littered with stale symbols nobody uses anymore... quite a sore sight, believe me! Plus they take up precious memory.</li> +<li>First, we are storing a symbol &quot;_fact_n&quot; in the registry. Wait, but there's no value? Correct, the value will be provided when the symbol is used, so like $0x5$$ $:fact$$. It's like saying that we are <em>expecting</em> a value on the stack (and here we are assuming it's an integer, but that's ok for this example).</li> +<li>Then three (!) quotations and the symbol {{sym-if}} will be pushed on the stack. Yep, you got that right: that's a good old if clause. The first quotation is the condition to be checked, then the <em>then</em> quotation, and finally the <em>else</em> quotation. Note how we can recursively call the $:fact$$ operator that we are just defining... mind-blowing, I am sure.</li> +<li>Finally, we need to free the temporary symbol $:_fact_n$$ using {{sym-#}}. It is always good practice to do so, otherwise The Registry will be littered with stale symbols nobody uses anymore... quite a sore sight, believe me! Plus they take up precious memory.</li> </ul> <p>OK! Now you gotta have enough.</p> <p>What?! Still no loops huh? Go read about it in the specification {{sym-while}} symbol... takes two quotations... one for the condition and the other for the body of the loop. Really not rocket science once you get used to The Stack and The Registry.</p>