You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>There’s also an <code>Index</code> function which takes an integer parameter and a list, returning the corresponding list index (indexed from zero of course).</p>
156
157
<p><code>Range</code> creates a list of numbers from a starting (inclusive) and an ending (exclusive) number. <code>For</code> is a higher order function that applied an operation to each element of a list – it is the loop for iterating over lists.</p>
<p>The functional programmers reading this are likely expecting a Fold or Reduce function next – which applies an operation to a list with an accumulator. However Cognate needs no fold function, as <code>For</code> can store intermediary values on the stack, acting like a fold.</p>
<p><code>Insert</code> returns the table with an extra key-value pair, <code>Remove</code> returns the table without a specified key, and <code>Has</code> checks whether a key is in the table.</p>
<p>Symbols can’t be modified in any way, but can be compared very efficiently (which is why they’re great keys for tables) and put into any data structure.</p>
248
249
<h2id="begin">Begin</h2>
249
-
<p>Earlier when we defined loops, you may have noticed something missing – the break statement (also continue, for that matter). Being a functional language, Cognate encourages avoiding control flow like this, but sometimes you’ve just gotta get out of a block early. Introducing <code>Begin</code> – this function takes a block parameter and evaluates it, passing it <em>another</em> block on the stack. Evaluating <em>that</em> block will jump you out of the original block. Confused? Here’s an example.</p>
250
+
<p>Earlier when we defined loops, you may have noticed something missing – the break statement. Being a functional language, Cognate discourages this sort of control flow, but sometimes you’ve just gotta get out of a block early. Introducing <code>Begin</code> – this function takes a block parameter and evaluates it, passing it <em>another</em> block on the stack. Evaluating <em>that</em> block will jump you out of the original block. Confused? Here’s an example.</p>
250
251
<divclass="code"><pre><code><spanstyle='font-style: italic;color: #969896'>~~ Let's print the numbers up to 100 in an unnecessarily complicated manner.</span>
<p><code>Begin</code> can also be used to implement a return statement to break out of a function early. An advantage of <code>Begin</code> over traditional programming languages’ break and return statements is that it gives fine-grained control over which block you break out of.</p>
263
+
<p>This essentially allows any control flow to have a break statement, not just loops. <code>Begin</code> can also be used to implement a return statement to break out of a function early.</p>
264
+
<divclass="code"><pre><code>
265
+
<spanstyle='font-style: italic;color: #969896'>~~ Inefficiently decrements a number 100 times</span>
266
+
<spanstyle='font-style: italic;color: #969896'>~~ If it reaches zero, returns zero instead.</span>
<p>An advantage of <code>Begin</code> over traditional programming languages’ break and return statements is that it gives fine-grained control over which block you break out of, since nested <code>Begin</code> statements can have their exit blocks bound to different names.</p>
263
284
<h2id="end">End</h2>
264
285
<p>Nope, there isn’t an <code>End</code> function. This is just the end of the tutorial.</p>
0 commit comments