<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>aqua-ified thoughts</title>
    <description>never developed nor fully solidified</description>
    <link>https://tcpc.me//</link>
    <atom:link href="https://tcpc.me//feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 27 Sep 2026 21:59:38 -0400</pubDate>
    <lastBuildDate>Sun, 27 Sep 2026 21:59:38 -0400</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>All about FIFOs</title>
        <description>&lt;p&gt;&lt;em&gt;Traditional RTL and Bluespec perspectives on skid buffers and friends&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/post-extensions/fifos/BlogHeader.drawio.svg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I’ll suppose you’re already familiar with the basic pipeline with stalling. Say, you have a complicated function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; you want to compute in hardware. You can break it up into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt; stages with a register between each pair of consecutive stages.&lt;/p&gt;

&lt;p&gt;The upstream producer might not have any data to feed into the pipeline, so the pipeline must take an &lt;strong&gt;input valid bit&lt;/strong&gt; to indicate whether the data is available. The downstream consumer might not be ready to take the result from the pipeline, so the pipeline also requires &lt;strong&gt;an input ready bit&lt;/strong&gt; to indicate whether it is being stalled.&lt;/p&gt;

&lt;p&gt;The pipeline generates an &lt;strong&gt;output valid bit&lt;/strong&gt; as part of its result. It also generates an &lt;strong&gt;output ready bit&lt;/strong&gt; for the upstream producer.&lt;/p&gt;

&lt;p&gt;Focusing on a single stage of the pipeline, if the stage has valid data but the consumer is not ready, then it needs to hold the data for another cycle, so it must disable its register write and stall its upstream. That is, the output ready bit is high when either the consumer is happily consuming (input ready is high) or there is no data to hold in the first place (valid is low).&lt;/p&gt;

&lt;p&gt;When a consumer stalls, only the last few stages leading up to that stage gets stalled.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/post-extensions/fifos/LongPipeline.drawio.svg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;forward data path&lt;/strong&gt; is nicely broken up by registers, keeping the clock frequency high. However, the &lt;strong&gt;backward control path&lt;/strong&gt; forms a long combinational chain.&lt;/p&gt;

&lt;p&gt;This motivates treating the register between two stages, together with its control logic, as a module in its own right, so we can design it better and more easily.&lt;/p&gt;

&lt;p&gt;Specifically, we are interested in building a data storage module, usually called a &lt;strong&gt;buffer&lt;/strong&gt;. Using Bluespec terminology, we say we are building a &lt;strong&gt;FIFO&lt;/strong&gt; (of size 1!) supporting two &lt;strong&gt;methods&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; (enqueue)&lt;/strong&gt;: takes a piece of data with a valid bit.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt; (dequeue)&lt;/strong&gt;: outputs a piece of data with a valid bit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The success of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; is dependent on the input valid bit and the output ready bit: When they are both high, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; is considered “executed.” Similarly, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt; is executed when the input ready bit and the output valid bit are both high.&lt;/p&gt;

&lt;h2 id=&quot;pipeline-buffer&quot;&gt;Pipeline buffer&lt;/h2&gt;

&lt;p&gt;We have already seen an example of such a module in the earlier diagram. This is called a &lt;strong&gt;pipeline buffer&lt;/strong&gt; or &lt;strong&gt;pipeline FIFO&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/post-extensions/fifos/PipelineFIFO.drawio.svg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In the diagram, the registers are called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid&lt;/code&gt;. The inputs are named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data_in&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_in&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_in&lt;/code&gt;. The outputs are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data_out&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_out&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_out&lt;/code&gt;. Note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_in&lt;/code&gt; refers to an input bit &lt;em&gt;coming from the output side&lt;/em&gt;! It’s confusing. I know. Whine about it or choose a different naming convention.&lt;/p&gt;

&lt;h3 id=&quot;pipeline-buffer-as-a-bluespec-module-with-guarded-interfaces&quot;&gt;Pipeline buffer as a Bluespec module with guarded interfaces&lt;/h3&gt;

&lt;p&gt;Here’s one way to understand the circuit using Bluespec concepts.&lt;/p&gt;

&lt;p&gt;Think of the producer and consumer as individual &lt;strong&gt;rules&lt;/strong&gt; that may or may not execute the methods &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt;, respectively.&lt;/p&gt;

&lt;p&gt;When the buffer is empty, only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; can execute. When the buffer is full, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt; can take data from the register, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; can replace it with new data in the same cycle.&lt;/p&gt;

&lt;p&gt;Observe that whether &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; can execute depends on whether &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt; has executed. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt; frees up the space, allowing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; to proceed. Even though they execute in the same cycle, it’s as if &lt;em&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt; happens a tiny bit before &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt;&lt;/em&gt;. This gives us a causal relationship where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt; precedes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt;. In Bluespec terms, we say &lt;strong&gt;the method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt; is “scheduled before” &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt;&lt;/strong&gt;, written &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq &amp;lt; enq&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Some designs also include a separate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first&lt;/code&gt; method for accessing the value. The only sensible schedule is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first &amp;lt; deq &amp;lt; enq&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Of course, these methods and rules are just abstractions. In the circuit itself, there’s no notion of “rules”—only a combinational path from the consumer side back to the producer side.&lt;/p&gt;

&lt;h3 id=&quot;pipeline-buffer-as-a-finite-state-machine&quot;&gt;Pipeline buffer as a finite state machine&lt;/h3&gt;

&lt;p&gt;We can also take a more traditional view. Think of this as a state machine with two states:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Empty&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!valid&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Full&lt;/strong&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The inputs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_in&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_in&lt;/code&gt; correspond to whether the producer has data and whether the consumer is ready.&lt;/p&gt;

&lt;p&gt;Here is the state transition diagram (outputs omitted):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/post-extensions/fifos/PipelineFSM.drawio.svg&quot; alt=&quot;&quot; width=&quot;100%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I’ve labeled key transitions &lt;strong&gt;fill&lt;/strong&gt;, &lt;strong&gt;flow&lt;/strong&gt;, and &lt;strong&gt;flush&lt;/strong&gt;, which should all be self-explanatory.&lt;/p&gt;

&lt;p&gt;The outputs can be written explicitly (read from the earlier circuit diagram):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_out = valid&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_out = !valid || ready_in&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_out&lt;/code&gt; depends not only on the state &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid&lt;/code&gt; but also on the input &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_in&lt;/code&gt;, this is a &lt;strong&gt;Mealy machine&lt;/strong&gt;. Again, we note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_out&lt;/code&gt; is a function of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_in&lt;/code&gt;, consistent with the Bluespec schedule &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq &amp;lt; enq&lt;/code&gt;, since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_out&lt;/code&gt; ultimately depends on the downstream’s readiness.&lt;/p&gt;

&lt;p&gt;Whew, I said the same things a hundred different ways, but I really want to emphasize their connections!&lt;/p&gt;

&lt;p&gt;A pipeline buffer is a natural choice for most pipelines, but some systems may require a different behavior, so now we’re going to look at…&lt;/p&gt;

&lt;h2 id=&quot;bypass-buffer&quot;&gt;Bypass buffer&lt;/h2&gt;

&lt;p&gt;Suppose we have two modules where the first module’s output is the second module’s input. In the ideal case, we want to do all computations in one cycle. However, the second module may occasionally stall, so we need to store the data temporarily. When the buffer holds data, we must stall the first module.&lt;/p&gt;

&lt;p&gt;Can we design a circuit for this? Pause and try for yourself if you’d like.&lt;/p&gt;

&lt;h3 id=&quot;finite-state-machine-and-bluespec&quot;&gt;Finite state machine and Bluespec&lt;/h3&gt;

&lt;p&gt;We can again think of this as a finite-state machine, but using some Bluespec intuition helps.&lt;/p&gt;

&lt;p&gt;We still have a buffer (with a valid bit) to store data when needed. The buffer can be either &lt;strong&gt;empty&lt;/strong&gt; or &lt;strong&gt;full&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When the buffer is empty:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;If the producer has data, it wants to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; should succeed.&lt;/li&gt;
  &lt;li&gt;We attempt to pass the data to the consumer via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first&lt;/code&gt; in the same clock cycle.&lt;/li&gt;
  &lt;li&gt;If the consumer accepts it (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt;), the data passes through without being stored. Otherwise, the data is saved into the buffer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The causality chain gives us the schedule &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq &amp;lt; first &amp;lt; deq&lt;/code&gt;. That makes sense: we want data to flow directly from upstream to downstream in the same cycle.&lt;/p&gt;

&lt;p&gt;When the buffer is full, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; can’t execute. We must first consume the stored data (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The resulting state transition diagram looks like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/post-extensions/fifos/BypassFSM.drawio.svg&quot; alt=&quot;&quot; width=&quot;100%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Compare it with the pipeline buffer FSM—you’ll see a nice symmetry.&lt;/p&gt;

&lt;h3 id=&quot;circuit-diagram-for-a-bypass-buffer&quot;&gt;Circuit diagram for a bypass buffer&lt;/h3&gt;

&lt;p&gt;Let’s reason out what the circuit must look like.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;There must be a &lt;strong&gt;combinational path&lt;/strong&gt; from input to output, since data can flow directly.&lt;/li&gt;
  &lt;li&gt;The outputs must pass through &lt;strong&gt;MUXes&lt;/strong&gt;, as they can come either from the input or from the buffer.&lt;/li&gt;
  &lt;li&gt;Finally, depending on whether the consumer is ready (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_in&lt;/code&gt;), we update the buffer accordingly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whenever &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_in&lt;/code&gt; is true, we clear the buffer—so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_in&lt;/code&gt; effectively acts as a local reset. Once the buffer takes a value, it holds it until reset.&lt;/p&gt;

&lt;p&gt;This leads to the following circuit diagram, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data_in&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_in&lt;/code&gt; and the internal &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid&lt;/code&gt; registers are shown together:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/post-extensions/fifos/BypassFIFO.drawio.svg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Just to be clear, my diagrams are for conceptual understanding only. Real world synthesis tools likely won’t abuse enable/reset ports the same way I did.&lt;/p&gt;

&lt;p&gt;Observe that this is the &lt;strong&gt;opposite&lt;/strong&gt; of the pipeline buffer: here, there’s a combinational path from &lt;strong&gt;upstream to downstream&lt;/strong&gt;, but the &lt;strong&gt;ready&lt;/strong&gt; signal from downstream is registered before reaching upstream.&lt;/p&gt;

&lt;h2 id=&quot;magic-buffer&quot;&gt;Magic buffer&lt;/h2&gt;

&lt;p&gt;In a pipeline buffer, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt; can execute concurrently only when the buffer is non-empty.&lt;br /&gt;
In a bypass buffer, they can execute concurrently only when the buffer isn’t full.&lt;/p&gt;

&lt;p&gt;Can we have the best of both worlds: maximum concurrency?&lt;/p&gt;

&lt;p&gt;Here’s an idea:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;When the buffer is empty, act like a bypass buffer.&lt;/li&gt;
  &lt;li&gt;When full, act like a pipeline buffer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This way, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt; could always execute concurrently!&lt;/p&gt;

&lt;p&gt;But there are two major problems.&lt;/p&gt;

&lt;p&gt;First, you can’t do this in Bluespec using guarded interfaces. Scheduling must be static, and this behavior has an obvious scheduling contradiction.
You could work around it using low-level interfaces like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CommitIfc&lt;/code&gt;, but you’d have to ensure that producer and consumer rules decide independently, risking combinational cycles and losing the benefits of guarded interfaces.&lt;/p&gt;

&lt;p&gt;Second, if there’s a combinational path in &lt;strong&gt;both directions&lt;/strong&gt;, what purpose does the buffer serve? I don’t know—there might be a legitimate use case for this; let me know if you have any ideas.&lt;/p&gt;

&lt;p&gt;The term &lt;strong&gt;“magic buffer”&lt;/strong&gt; is my own (i.e., not in the literature). It is magical—maximum concurrency with one slot—but it’s ultimately an illusion. There is no free lunch.&lt;/p&gt;

&lt;h2 id=&quot;half-buffer&quot;&gt;Half buffer&lt;/h2&gt;

&lt;p&gt;Here’s another way to interpret “the best of both worlds”: no combinational paths in either direction.&lt;/p&gt;

&lt;p&gt;The idea is to have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_out&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_out&lt;/code&gt; depend solely on internal state:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_out = !valid&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_out = valid&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s the state machine:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/post-extensions/fifos/HalfBufferFSM.drawio.svg&quot; alt=&quot;&quot; width=&quot;100%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The circuit (not shown) can be described as:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data &amp;lt;= valid ? data : data_in&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid &amp;lt;= valid ? !ready_in : valid_in&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We call this a &lt;strong&gt;half buffer&lt;/strong&gt; because it achieves only half the ideal throughput. Even if the producer always has valid data, it can only pass data every two cycles. In contrast, pipeline and bypass buffers have a throughput of 1. There is no free lunch.&lt;/p&gt;

&lt;h2 id=&quot;skid-buffer&quot;&gt;Skid buffer&lt;/h2&gt;

&lt;p&gt;For the third time, &lt;strong&gt;can we have the best of both worlds?&lt;/strong&gt; That is, we want maximum concurrency &lt;em&gt;and&lt;/em&gt; no combinational paths in either direction.&lt;/p&gt;

&lt;p&gt;The answer is yes. And there is no free lunch—we pay with extra storage.&lt;/p&gt;

&lt;h3 id=&quot;skidding-to-a-stop&quot;&gt;Skidding to a stop&lt;/h3&gt;

&lt;p&gt;Recall the pipeline buffer. Its forward data path already uses registers, so that’s fine. It’s the backward control path that forms a long combinational chain.&lt;/p&gt;

&lt;p&gt;What if, instead of setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_out = !valid || ready_in&lt;/code&gt; combinationally, we register it: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready &amp;lt;= !valid || ready_in; ready_out = ready.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This removes the combinational path backward but introduces a problem.&lt;/p&gt;

&lt;p&gt;If data is flowing continuously and downstream suddenly stalls (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_in&lt;/code&gt; becomes false), the producer still sees &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_out = true&lt;/code&gt; for one extra cycle. The buffer will accept new data it can’t store, losing it, just like me accepting more work when my plate is full.&lt;/p&gt;

&lt;p&gt;To fix this, we add an extra data slot. When the main slot is full and we’re about to stall, the new data goes into the extra slot instead. Until that slot is drained, we deassert &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_out&lt;/code&gt;. The data effectively &lt;em&gt;skids to a stop&lt;/em&gt;—hence the name &lt;strong&gt;skid buffer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here’s the state machine:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/post-extensions/fifos/SkidBufferFSM.drawio.svg&quot; alt=&quot;&quot; width=&quot;120%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Outputs can be defined simply as:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_out = (state != FULL)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_out = (state != EMPTY)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This gives us a &lt;strong&gt;Moore machine&lt;/strong&gt;—no dependence on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;valid_in&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ready_in&lt;/code&gt;!&lt;/p&gt;

&lt;h3 id=&quot;equivalence-to-a-bypass-buffer-and-a-pipeline-buffer&quot;&gt;Equivalence to a bypass buffer and a pipeline buffer&lt;/h3&gt;

&lt;p&gt;Finding the exact definition for the updated register values is tricky. You can try to do it yourself. I’m lazy, so I just observe that the behavior is equivalent to &lt;strong&gt;a bypass buffer followed by a pipeline buffer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;See:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;When both slots are empty, new data passes through the bypass buffer and fills the pipeline buffer.&lt;/li&gt;
  &lt;li&gt;When only the main slot is full, the behavior depends on whether downstream consumes or stalls.&lt;/li&gt;
  &lt;li&gt;When both are full, the downstream consumes first, and data shifts from the bypass buffer into the pipeline buffer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thus, the behavior matches exactly what we want.&lt;/p&gt;

&lt;p&gt;Here’s the combined diagram:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/post-extensions/fifos/SkidBuffer.drawio.svg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This combination gives us the best of both worlds:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The &lt;strong&gt;pipeline buffer&lt;/strong&gt; breaks the forward combinational path.&lt;/li&gt;
  &lt;li&gt;The &lt;strong&gt;bypass buffer&lt;/strong&gt; breaks the backward path by providing slack when stalls arrive late.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We could also combine them in the reverse order (pipeline then bypass); it’s less intuitive but still correct.&lt;/p&gt;

&lt;p&gt;Of course, the tradeoff is twice the storage elements.&lt;/p&gt;

&lt;h3 id=&quot;conflict-free-fifo&quot;&gt;Conflict-free FIFO&lt;/h3&gt;

&lt;p&gt;In Bluespec, this structure is called a &lt;strong&gt;conflict-free FIFO&lt;/strong&gt;, typically of size 2 (since size 1 makes no sense). It’s “conflict-free” because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt; are independent. The schedule is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first &amp;lt; {enq, deq}&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq CF deq&lt;/code&gt; (CF stands for conflict-free).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;resources&quot;&gt;Resources&lt;/h2&gt;

&lt;p&gt;References I used while writing this:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://fpgacpu.ca/fpga/index.html&quot;&gt;&lt;em&gt;FPGA Design Elements&lt;/em&gt;&lt;/a&gt; by Charles Eric LaForest—especially the “Elastic Pipelines” section.&lt;/li&gt;
  &lt;li&gt;ZipCPU’s &lt;a href=&quot;https://zipcpu.com/blog/2017/08/14/strategies-for-pipelining.html&quot;&gt;&lt;em&gt;Strategies for Pipelining Logic&lt;/em&gt;&lt;/a&gt; (2017) and &lt;a href=&quot;https://zipcpu.com/blog/2019/05/22/skidbuffer.html&quot;&gt;&lt;em&gt;Building a Skid Buffer for AXI Processing&lt;/em&gt;&lt;/a&gt; (2019).&lt;/li&gt;
  &lt;li&gt;Arvind et al., &lt;em&gt;Introduction to Digital Design as Cooperating Sequential Machines&lt;/em&gt; (&lt;a href=&quot;https://csg.csail.mit.edu/6.375/6_375_2019_www/resources/Logic_Design.pdf&quot;&gt;2019 draft&lt;/a&gt;).&lt;/li&gt;
  &lt;li&gt;Arvind et al., &lt;em&gt;Computer Architecture: A Constructive Approach&lt;/em&gt; (&lt;a href=&quot;https://csg.csail.mit.edu/6.175/resources/archbook_2015-08-25.pdf&quot;&gt;2015 draft&lt;/a&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://csg.csail.mit.edu/6.175/labs/lab4-fifo.html&quot;&gt;6.175 Fall 2017 Lab 4&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Special thanks to &lt;a href=&quot;https://people.csail.mit.edu/bthom/&quot;&gt;Thomas Bourgeat&lt;/a&gt;, who taught 6.1920 (6.175) Constructive Computer Architecture in Spring 2023, when I could fortunately take it, and to the late &lt;a href=&quot;https://csg.csail.mit.edu/Users/arvind/&quot;&gt;Arvind&lt;/a&gt;, whose work underpins much of Bluespec.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;appendix&quot;&gt;Appendix&lt;/h2&gt;

&lt;p&gt;Rants and notes that didn’t fit in the main post.&lt;/p&gt;

&lt;h3 id=&quot;skid-buffer-1&quot;&gt;“Skid” buffer&lt;/h3&gt;

&lt;p&gt;Sometimes, people refer to our bypass buffer as a skid buffer. There’s no standard name in this messy field.&lt;/p&gt;

&lt;h3 id=&quot;larger-fifos&quot;&gt;Larger FIFOs&lt;/h3&gt;

&lt;p&gt;In Bluespec world, for larger FIFOs, we expect &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deq&lt;/code&gt; to execute concurrently whenever the FIFO isn’t empty or full.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;A &lt;strong&gt;pipeline FIFO&lt;/strong&gt; also allows concurrent operations when full (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first &amp;lt; deq &amp;lt; enq&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;A &lt;strong&gt;bypass FIFO&lt;/strong&gt; allows concurrency when empty (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq &amp;lt; first &amp;lt; deq&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;A &lt;strong&gt;conflict-free FIFO&lt;/strong&gt; satisfies &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first &amp;lt; {enq, deq}&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enq CF deq&lt;/code&gt;, but doesn’t require concurrency in the full/empty cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The implication is:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Pipeline FIFO -&amp;gt; long backward combinational path.&lt;/li&gt;
  &lt;li&gt;Bypass FIFO -&amp;gt; long forward combinational path.&lt;/li&gt;
  &lt;li&gt;Conflict-free FIFO -&amp;gt; fully isolated, but no concurrency in full/empty cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;larger-but-still-small-fifos&quot;&gt;Larger but Still Small FIFOs&lt;/h3&gt;

&lt;p&gt;You can’t simply chain &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; bypass buffers to make a size-&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; FIFO. When a stall occurs, data ripples backward through the chain, breaking concurrency guarantees. Similarly, chaining pipeline buffers creates a &lt;strong&gt;delay line&lt;/strong&gt;, not a flexible FIFO.&lt;/p&gt;

&lt;p&gt;Skid buffers chained together are even more wasteful.&lt;/p&gt;

&lt;p&gt;To maintain concurrency, you need a proper parameterized FIFO with a shift register and plenty of extra logic—or you can just leverage magic buffers from earlier.&lt;/p&gt;

&lt;p&gt;This shows why building large FIFOs purely from registers quickly becomes impractical. There is no free lunch.&lt;/p&gt;

&lt;h3 id=&quot;actually-large-fifos&quot;&gt;Actually Large FIFOs&lt;/h3&gt;

&lt;p&gt;Realistic designs resemble software circular buffers with read and write pointers. Achieving pipeline, bypass, or conflict-free semantics requires clever implementation details (see references for examples).&lt;/p&gt;

&lt;h3 id=&quot;ephemeral-history-registers-ehrs&quot;&gt;Ephemeral History Registers (EHRs)&lt;/h3&gt;

&lt;p&gt;With those primitive FIFOs, there are few legitimate reasons to use EHRs. They’re difficult to get right, and most designs can be expressed more cleanly using rules and FIFOs.&lt;/p&gt;
</description>
        <pubDate>Wed, 22 Oct 2025 00:30:00 -0400</pubDate>
        <link>https://tcpc.me//2025/10/22/fifos.html</link>
        <guid isPermaLink="true">https://tcpc.me//2025/10/22/fifos.html</guid>
        
        
      </item>
    
      <item>
        <title>PSA: Design if-elses with priority</title>
        <description>&lt;p&gt;&lt;em&gt;Public service announcement for 6.191 students struggling with lab 6 (and others).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One thing consistently trips up students in lab 6 is the lack of understanding of logic. Actually, you might have already learned this before, but half of you will somehow forget about it in this lab. This post is a reminder.&lt;/p&gt;

&lt;p&gt;Suppose you have a register &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data&lt;/code&gt;, input booleans &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enable&lt;/code&gt; and input data &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data_in&lt;/code&gt;, and you want to write to a register only when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enable&lt;/code&gt; is high, and you also want to zero out the register if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset&lt;/code&gt; is asserted. Here’s one way you could write the code:&lt;/p&gt;
&lt;div class=&quot;language-verilog highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// GOOD&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is &lt;em&gt;not&lt;/em&gt; the same as if you were to write:&lt;/p&gt;
&lt;div class=&quot;language-verilog highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// WRONG&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the first excerpt, if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset&lt;/code&gt; is asserted, then it doesn’t matter what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enable&lt;/code&gt; is; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data&lt;/code&gt; will be zeored out. This is probably an acceptable behavior.&lt;/p&gt;

&lt;p&gt;In the second excerpt, if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enable&lt;/code&gt; is asserted, then an update always happens with no reset! This behavior &lt;em&gt;might&lt;/em&gt; make sense in some rare cases, but more likely than not, it’s just a bug.&lt;/p&gt;

&lt;h2 id=&quot;implicit-conditions&quot;&gt;Implicit conditions&lt;/h2&gt;

&lt;p&gt;Let’s solidify this understanding with the idea of “implicit conditions.” Every basic block in the code has a condition under which it will execute. Under an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;else&lt;/code&gt; block, the precise condition is the negation of its preceding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;Applying this concept to the first excerpt, you can think of it as having these three mutually exclusive branches:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!reset &amp;amp;&amp;amp; enable&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!reset &amp;amp;&amp;amp; !enable&lt;/code&gt; (which does not update &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can think of the first branch as handling two possibilities at the same time: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset &amp;amp;&amp;amp; enable&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset &amp;amp;&amp;amp; !enable&lt;/code&gt;. So, in total, we have four possibilities, just like in a truth table.&lt;/p&gt;

&lt;p&gt;The excerpt can then be re-written as:&lt;/p&gt;
&lt;div class=&quot;language-verilog highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// correct but verbose&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// do nothing&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;All four cases could be safely re-ordered, but this is unnecessarily verbose.&lt;/p&gt;

&lt;h2 id=&quot;designing-with-priority&quot;&gt;Designing with priority&lt;/h2&gt;

&lt;p&gt;Design your if-else chains to reflect your priorities when making decisions.&lt;/p&gt;

&lt;p&gt;Here’s a good example of what &lt;strong&gt;not&lt;/strong&gt; to do:&lt;/p&gt;
&lt;div class=&quot;language-verilog highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// correct but unreadable&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Exercise:&lt;/strong&gt; Convince yourself that it is equivalent to the first code excerpt using the idea of implicit conditions and some boolean properties. Or… just write out a truth table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; The three branches are&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!reset &amp;amp;&amp;amp; enable&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!(!reset &amp;amp;&amp;amp; enable) &amp;amp;&amp;amp; reset&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;By De Morgan’s law, we have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(reset || !enable) &amp;amp;&amp;amp; reset&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;By absorption, we have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!(!reset &amp;amp;&amp;amp; enable) &amp;amp;&amp;amp; !reset&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;By De Morgan’s law, we have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(reset || !enable) &amp;amp;&amp;amp; !reset&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;By distributivity, we have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset &amp;amp;&amp;amp; !reset || !enable &amp;amp;&amp;amp; !reset&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;Complement on the left branch gives us &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;False&lt;/code&gt;, which is an identity for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;||&lt;/code&gt;, which means we simply ahve &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!enable &amp;amp;&amp;amp; !reset&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;Or you can write this as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!reset &amp;amp;&amp;amp; !enable&lt;/code&gt;.
You can now see that the three branches’ conditions agree with the previous analysis, but this is horrid. The first code excerpt was much better because it clearly showed that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset&lt;/code&gt; was prioritized.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Exercise:&lt;/strong&gt; Convince yourself that this is &lt;strong&gt;not&lt;/strong&gt; equivalent to the first code excerpt.&lt;/p&gt;
&lt;div class=&quot;language-verilog highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; The second branch is essentially &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset &amp;amp;&amp;amp; enable&lt;/code&gt;. Therefore, the last branch is just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!enable&lt;/code&gt; where we do not update &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data&lt;/code&gt; whether &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reset&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!reset&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This might be a legit behavior in some circumstances, but it’s hard to read. Can you find a way to write this in a more readable way?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: The most important thing is whether the register is enabled or not. Then, we can decide whether to reset or not reset. This structure reflects our priority:&lt;/p&gt;
&lt;div class=&quot;language-verilog highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you really must avoid nested-ifs, then consider:&lt;/p&gt;
&lt;div class=&quot;language-verilog highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// do nothing&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In general, keep your conditions simple. Try not to combine &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;||&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;else if&lt;/code&gt; because that almost always means doing De Morgan’s law in your head every time you read through the code.&lt;/p&gt;
</description>
        <pubDate>Wed, 15 Oct 2025 18:00:00 -0400</pubDate>
        <link>https://tcpc.me//2025/10/15/psa-if-else.html</link>
        <guid isPermaLink="true">https://tcpc.me//2025/10/15/psa-if-else.html</guid>
        
        
      </item>
    
      <item>
        <title>Math rabbit hole: Decaying lights</title>
        <description>&lt;p&gt;Here’s a fun problem that I came up with a while ago.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Suppose you have $n$ lightbulbs. All of them are initially on.
On every second, each lightbulb &lt;a href=&quot;https://en.wikipedia.org/wiki/Independence_(probability_theory)&quot;&gt;independently&lt;/a&gt; has probability $p$ of turning off permanently.
What is the &lt;a href=&quot;https://en.wikipedia.org/wiki/Expected_value&quot;&gt;expected&lt;/a&gt; amount of time you have to wait for all light bulbs to die?
\(\newcommand{ev}[1]{\mathbb{E}\left[#1\right]}
\newcommand{evt}[1]{\ev{T_{p}(#1)}}
\newcommand{binom}[2]{ {#1 \choose #2} }
\newcommand{bigo}[1]{\mathrm{O}\!\left( #1 \right) }
\newcommand{prob}[1]{\mathbb{P}\!\left( #1 \right) }\)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s see, if we have $n=1$ lightbulb and $p=0.5$, then the expected time is 2 seconds. If we have $n = 2$ lightbulbs, the expected time is definitely longer than 2 seconds. It is also shorter than 4 seconds because we aren’t just considering one light bulb at a time. There’s a chance that both lightbulbs go off at once, or maybe one lightbulb goes off, making it possible for us to just focus on the one remaining light bulb.&lt;/p&gt;

&lt;p&gt;Our goal is to find a formula for computing $\evt{n} = \,$ the expected amount of time for $n$ lightbulbs to die, given that the probability of dying at each second is $p$.&lt;/p&gt;

&lt;p&gt;Depending on your background, this might seem like a very simple problem or a very difficult problem. &lt;strong&gt;Either way, I highly encourage you to try this problem for yourself.&lt;/strong&gt;  The problem is surprisingly deep, and it’s just a very cute problem.&lt;/p&gt;

&lt;p&gt;From this point onward are my solution&lt;strong&gt;s&lt;/strong&gt;, including all the steps and detours along the way. This follows pretty much the exact path I’ve taken.&lt;/p&gt;

&lt;h2 id=&quot;the-competitive-programmers-brain-rot&quot;&gt;The competitive programmer’s brain-rot&lt;/h2&gt;

&lt;p&gt;My first instinct as someone who’s been involved with way too many algorithmic interviews recently is to use &lt;a href=&quot;https://en.wikipedia.org/wiki/Dynamic_programming&quot;&gt;dynamic programming&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Consider this: In the beginning, we have $n$ lightbulbs. We take one second, and boom, we have $n-k$ lightbulbs where $k$ is some number between 0 and $n$. Because the lightbulbs are &lt;a href=&quot;https://en.wikipedia.org/wiki/Memorylessness&quot;&gt;memoryless&lt;/a&gt;, we have essentially the same problem or subproblem. We can just use this answer plus the one second it takes for the first step. We don’t know what $k$ is, so we have to consider all possibilities and find the expected value.&lt;/p&gt;

&lt;h3 id=&quot;example-computation&quot;&gt;Example computation&lt;/h3&gt;

&lt;p&gt;Let’s get a sense of how this works with $p=0.5$.&lt;/p&gt;

&lt;p&gt;Obviously, $\evt{0} = 0$ and $\evt{1} = (1) = 2$.&lt;/p&gt;

&lt;p&gt;Now, to compute $\evt{2}$, we observe that after one second has passed, there are three cases (or four equally likely scenarios):&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;No light turns off. This happens with probability $0.25$, and we still need to take an expected time of $\evt{2}$ seconds from here to complete.&lt;/li&gt;
  &lt;li&gt;Exactly one light turns off. This happens with a probability of $0.5$ (either one can turn off!). We need to take an expected time of $\evt{1}$ seconds here to complete.&lt;/li&gt;
  &lt;li&gt;Both lights turn off. This happens with probability $0.25$, and we’re done, i.e. taking $\evt{0} = 0$ seconds.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From this, we can write&lt;/p&gt;

\[\evt{2} = 1 + \left( \frac{1}{4} \evt{2} + \frac{1}{2} \evt{1} + \frac{1}{4} \evt{0} \right),\]

&lt;p&gt;which we can solve into&lt;/p&gt;

\[\begin{align}
\left( 1 - \frac{1}{4}  \right) \evt{2} &amp;amp;= 1 + \frac{1}{2} \evt{1} + \frac{1}{4} \evt{0} \\
\frac{3}{4} \evt{2} &amp;amp;= 1 + \frac{1}{2}\left( 2 \right) + \frac{1}{4}\left( 0 \right) = 2 \\
\evt{2} &amp;amp;= \frac{8}{3} \approx 2.667.
\end{align}\]

&lt;p&gt;This agrees with our intuition that the answer is between 2 and 4. It turns out to be less than 3, which also feels quite right.&lt;/p&gt;

&lt;p&gt;We can do this again with $n=3$, but it’s now a bit harder because there are eight equally likely scenarios.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;No light turns off. This is a 1 in 8 chance.&lt;/li&gt;
  &lt;li&gt;Exactly one light turns off. There are three ways for this to happen, one for each light.&lt;/li&gt;
  &lt;li&gt;Exactly two lights turn off. There are also three ways for this to happen, one for each light that doesn’t turn off.&lt;/li&gt;
  &lt;li&gt;All lights turn off.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is,
\(\begin{align}
\evt{3} &amp;amp;= 1 + \left( \frac{1}{8} \evt{3} + \frac{3}{8} \evt{2} + \frac{3}{8} \evt{1} + \frac{1}{8} \evt{0}] \right)  \\
\left( 1 - \frac{1}{8} \right) \evt{3} &amp;amp;= 1 + \frac{3}{8} \left( \frac{8}{3} \right)  + \frac{3}{8}\left( 2 \right) \\
\frac{7}{8} \evt{3} &amp;amp;= 1 + 1 + \frac{3}{4} = \frac{11}{4} \\
\evt{3} &amp;amp;= \frac{11}{4} \cdot \frac{8}{7} = \frac{22}{7} \approx 3.143.
\end{align}\)&lt;/p&gt;

&lt;p&gt;Huh. Kinda funny $\frac{22}{7}$ shows up here. It’s just a coincidence, though. The next number is $\frac{368}{105} \approx 3.505$.&lt;/p&gt;

&lt;h3 id=&quot;the-general-solution&quot;&gt;The general solution&lt;/h3&gt;

&lt;p&gt;Anyhow, there are a few things that should start being obvious at this point. For one, the answer definitely involves &lt;a href=&quot;https://en.wikipedia.org/wiki/Binomial_coefficient&quot;&gt;binomial coefficients&lt;/a&gt;, i.e. &lt;a href=&quot;https://en.wikipedia.org/wiki/Pascal%27s_triangle&quot;&gt;Pascal’s Triangle&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To generalize to an arbitrary value of $p$, you’ll also need to know about &lt;a href=&quot;https://en.wikipedia.org/wiki/Binomial_distribution&quot;&gt;the binomial distribution&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A quick recap: if you have $n$ items and you &lt;a href=&quot;https://en.wikipedia.org/wiki/Bernoulli_trial&quot;&gt;independently choose, for each item, whether to include it or not, with probability $p$&lt;/a&gt;, then the probability that you picked exactly $k$ items is&lt;/p&gt;

\[\binom{n}{k} p^k q^{n-k}\]

&lt;p&gt;where we write $q = 1-p$ for convenience. (Observe that in earlier examples, we have $p = q = 0.5$ so that’s why we have the $\frac{1}{2^n}$ factor showing up in the denominator, representing $2^n$ equally likely scenarios.)&lt;/p&gt;

&lt;p&gt;Using this, we can write the general formula for $\evt{n}$ quite easily.&lt;/p&gt;

\[\evt{n} = 1 + \sum_{k=0}^{n} \binom{n}{k}p^kq^{n-k} \evt{n-k},\]

&lt;p&gt;but we’re not done yet! Don’t forget that $\evt{n}$ shows up on both the left-hand side and the right-hand side, so you’ll have to rearrange a little bit:&lt;/p&gt;

\[\begin{align}
\evt{n} = 1 + \sum_{k=0}^{n} \binom{n}{k}p^kq^{n-k} \evt{n-k} \\
\left( 1 - \binom{n}{0}p^0q^n \right) \evt{n} = 1 + \sum_{k=1}^{n} \binom{n}{k}p^kq^{n-k} \evt{n-k} \\
\left( 1 - q^n \right) \evt{n} = 1 + \sum_{k=1}^{n} \binom{n}{k}p^kq^{n-k} \evt{n-k} \\
\end{align}\]

&lt;p&gt;Finally, dividing by $1-q^n$, we have:&lt;/p&gt;

\[\boxed{\evt{n} = \frac{ 1 + \sum_{k=1}^{n} \binom{n}{k}p^kq^{n-k} \evt{n-k} }{1-q^n}}.\]

&lt;p&gt;The formula looks a bit scary to compute, but we could just code this up!&lt;/p&gt;

&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;functools&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;functools&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;recurrence_solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# expected time if there are n lights
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# ET[n] = 1 + sum(ET[n-k] * p**k + q**(n-k) * C(n,k)  for 0 &amp;lt;= k &amp;lt;= n)
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# (1 - q**n) ET[n]  =  1 + sum(ET[n-k] * p**k * q**(n-k)  for 1 &amp;lt;= k &amp;lt;= n)
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recurrence_solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The result is&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0 0
1 2.0
2 2.6666666666666665
3 3.142857142857143
4 3.5047619047619047
5 3.794162826420891
6 4.034818228366615
7 4.240848926142298
8 4.421077725815582
9 4.581310192143427
10 4.725559323634528
11 4.856722010080972
12 4.976966144409677
13 5.087961538668442
14 5.191024016159364
15 5.28720947379643
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Pretty cool. It seems the result grows rather slowly. In fact, it seems like it grows at the rate of $\bigo{\log n}.$ Here, in this graph, I set the factor so that the graph crosses at $n=100$, so from that point onward, $\ev{T_{1/2}(n)} \leq 1.202 \log_{2} n$.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/decaying-lights/graph.png&quot; alt=&quot;&quot; width=&quot;80%&quot; /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;its-not-enough&quot;&gt;It’s not enough.&lt;/h3&gt;

&lt;p&gt;Okay, so, there are a few unsatisfying things about this solution.&lt;/p&gt;

&lt;p&gt;For one, you can’t just compute $\evt{n}$ for any $n$ you want. You need to also compute it for all the lower values of $n$. Each computation takes $\bigo{n}$ time so overall you spend $\bigo{n^2}$ time. Can we do better?&lt;/p&gt;

&lt;p&gt;Secondly, it just looks scary. Is there an easier solution?&lt;/p&gt;

&lt;p&gt;Indeed, there is, but let’s take a step back for the moment. There’s an interesting connection that I want to point out.&lt;/p&gt;

&lt;h2 id=&quot;detour-radioactive-decay&quot;&gt;Detour: Radioactive decay&lt;/h2&gt;

&lt;p&gt;In the problem I discussed, the lightbulbs decide whether to turn off or not on each discrete second. In real life, they probably act in a more “continuous” manner. Is there a way we can turn this problem into a continuous version? Indeed, there is!&lt;/p&gt;

&lt;p&gt;In the discrete version, the time it takes for a single lightbulb to turn off follows a &lt;a href=&quot;https://en.wikipedia.org/wiki/Geometric_distribution&quot;&gt;geometric distribution&lt;/a&gt;. The continuous analog of this is called an &lt;a href=&quot;https://en.wikipedia.org/wiki/Exponential_distribution&quot;&gt;exponential distribution&lt;/a&gt;. Extending this to $n$ lightbulbs is like having $n$ radioactive atoms and then asking about how long we would expect before all atoms finally decay.&lt;/p&gt;

&lt;p&gt;Spoiler: The solution is a lot cleaner than the mess we found earlier.&lt;/p&gt;

&lt;h3 id=&quot;geometric-vs-exponential-distribution&quot;&gt;Geometric vs. exponential distribution&lt;/h3&gt;

&lt;p&gt;Let’s start with just one lightbulb or one atom first.&lt;/p&gt;

&lt;p&gt;In the &lt;a href=&quot;https://en.wikipedia.org/wiki/Geometric_distribution&quot;&gt;geometric distribution&lt;/a&gt;, you can compute the probability that the lightbulb (let’s say, lightbulb numbered $i$) takes exactly $t$ seconds to turn off as&lt;/p&gt;

\[\prob{T_{i} = t} = q^{t-1}p,\]

&lt;p&gt;or intuitively, the lightbulb fails to turn off for the first $t-1$ seconds with probability $q = 1-p$ each second (independently) then successfully takes the probability $p$ that turns it off at the last second.&lt;/p&gt;

&lt;p&gt;Oh, note on notation: $T_i$ is what we call a geometric random variable.&lt;/p&gt;

&lt;p&gt;Perhaps, it might be easier to think of this in terms of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Cumulative_distribution_function&quot;&gt;cumulative distribution function (CDF)&lt;/a&gt;, the probability that the lightbulb dies within the first $t$ second:&lt;/p&gt;

\[\prob{T_{i} \leq t} = 1-q^{t}.\]

&lt;p&gt;(Sanity check: $t=0$ means $\prob{T_{i} = 0} = 0$, which makes sense. A lightbulb only ever dies at time $t=1, 2, 3, \cdots$. At $t\to \infty$, the probability approaches 1.)&lt;/p&gt;

&lt;p&gt;In the &lt;a href=&quot;https://en.wikipedia.org/wiki/Exponential_distribution&quot;&gt;exponential distribution&lt;/a&gt;, instead of using the geometric sequence, you use, &lt;em&gt;*drum rolls*&lt;/em&gt;, exponential sequence! That is, the exponential random variable $T_i$ has&lt;/p&gt;

\[\prob{T_{i} \leq t} = 1 - e^{-\lambda_{i} t},\]

&lt;p&gt;where $\lambda_{i}$  is the term that controls the rate of decay; the higher $\lambda_{i}$ is, the more eager the lightbulb is to turn off. We can calculate that $\ev{T_{i}} = \frac{1}{\lambda}$.&lt;/p&gt;

&lt;p&gt;Intuitively, an exponential distribution is the limit of geometric progression but with increasingly fine-grained time steps. The probability of success at each time step is very low, but you can set it so that, cumulatively, the probability of success in one second or the expected time to succeed is exactly what you want.&lt;/p&gt;

&lt;p&gt;You can’t have both, though! Notice that if you set $\prob{T_{i}\leq1} = 0.5$, then $\lambda_{i} = 0.693$, which means the expected time is $1.443$ seconds. Continuous means there’s more chance for the light to turn off in general, so this is faster than the $2$ seconds we expect in the discrete version.&lt;/p&gt;

&lt;p&gt;Similarly, if you want the expected time to be exactly $2$ seconds, then the probability of not finishing within 1 second also has to rise too! The probability of finishing within 1 second falls to $0.393$.&lt;/p&gt;

&lt;h3 id=&quot;maximum-of-exponential-variables&quot;&gt;Maximum of exponential variables&lt;/h3&gt;

&lt;p&gt;Okay, back to the continuous analog of the original problem. We have $n$ lightbulbs. Light bulb $i$ turns off at time $T_i$. $T_1, T_2, \cdots, T_{n}$ are &lt;a href=&quot;https://en.wikipedia.org/wiki/Independent_and_identically_distributed_random_variables&quot;&gt;independent and identically distributed (i.i.d.)&lt;/a&gt; exponential random variables. What’s the expected time for all light bulbs to be turned off? That is, what is the expected value of $\max \left\{ T_{1}, T_{2}, \cdots, T_{n} \right\}$? &lt;a href=&quot;https://math.mit.edu/~sheffield/2019600/Lecture19.pdf#page=61&quot;&gt;This might be familiar to you if you’ve taken the same probability class as I have.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is tricky, so let’s flip the problem around. If we have $n$ light bulbs, what does the distribution look like for $\min \left\{ T_{1}, T_{2}, \cdots, T_{n} \right\}$? That is, we only care about the time it takes for &lt;em&gt;any&lt;/em&gt; light bulb at all to turn off.&lt;/p&gt;

&lt;p&gt;The cool thing is, with exponential random variables, the decay rates are additive! If you have $n$ i.i.d. exponential random variables, then their minimum is just an exponential random variable with the summed decay rate.&lt;/p&gt;

&lt;p&gt;Therefore, with $n$ light bulbs, the time to wait for any light to turn off is an exponential random variable with $\lambda = n\lambda_{i}$. The expected time is thus simply $\frac{1}{n}$ of one light bulb’s expected time.&lt;/p&gt;

&lt;p&gt;We care about the time for &lt;em&gt;all&lt;/em&gt; lightbulbs to turn off, though. Leveraging the memoryless property, we can simply add up:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The time it takes for one of the $n$ lightbulbs to turn off&lt;/li&gt;
  &lt;li&gt;The time it takes for one of the remaining $n-1$ lightbulbs to turn off&lt;/li&gt;
  &lt;li&gt;The time it takes for one of the remaining $n-2$ lightbulbs to turn off&lt;/li&gt;
  &lt;li&gt;and so on… until the time it takes for the final lightbulb to turn off.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Therefore, the formula for $\ev{\widetilde{T_{\lambda}}(n)}$, the expected time for all $n$ lightbulbs to turn off in the continuous version, is&lt;/p&gt;

\[\begin{align}
\ev{\widetilde{T_{\lambda}}(n)} &amp;amp;= \ev{\widetilde{T_{\lambda}}(1)} \left( 1 + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{n} \right)  \\ \\
&amp;amp;= \ev{\widetilde{T_{\lambda}}(1)} \cdot H_{n},
\end{align}\]

&lt;p&gt;where $H_n$ is the $n$-th &lt;a href=&quot;https://en.wikipedia.org/wiki/Harmonic_number&quot;&gt;harmonic number&lt;/a&gt;. The numbers clearly don’t match our discrete version, regardless of how we tune $\lambda$.&lt;/p&gt;

&lt;p&gt;Oh, yeah! Another thing to mention: It is well known that $H_{n}$ is in $\bigo{\log n}$. It grows at roughly the same pace as the discrete version.&lt;/p&gt;

&lt;h2 id=&quot;a-mathematicians-approach&quot;&gt;A mathematician’s approach&lt;/h2&gt;

&lt;p&gt;Alright, let’s remember the original problem. We have $n$ lightbulbs. Each might turn off, at each second, with probability $p$. How long does it take for all to turn off, on average?&lt;/p&gt;

&lt;p&gt;I asked my mathematician friends to tackle this problem. They don’t have the code-monkey brain-rot symptoms that I do, and sure enough, they found a pretty nice solution.&lt;/p&gt;

&lt;p&gt;First, if you’ve done a lot of probability problems, you may be aware of this formula:&lt;/p&gt;

\[\evt{n} = \sum_{t=0}^{\infty} \prob{T_{p}(n) &amp;gt; t}.\]

&lt;p&gt;Intuitively, we can compute the expected time by adding the probability that we have to take more than 0 seconds, more than 1 second, more than 2 seconds, and so on. In other words, we’re adding the &lt;a href=&quot;https://en.wikipedia.org/wiki/Survival_function&quot;&gt;survival function&lt;/a&gt; evaluated at each time step. This works because each of them contributes one second, scaled down to whatever probability they happen. Or, if you’d prefer it mathematically, from the definition of expected value:&lt;/p&gt;

\[\begin{align}
\evt{n} &amp;amp;= \sum_{t=0}^{\infty} \prob{T_{p}(n) = t} \cdot t \\
&amp;amp;= \hphantom{+} \prob{T_{p}(n) = 1} \\
&amp;amp;\,\hphantom{=} + \prob{T_{p}(n) = 2} + \prob{T_{p}(n) = 2} \\
&amp;amp;\,\hphantom{=} + \prob{T_{p}(n) = 3} + \prob{T_{p}(n) = 3} + \prob{T_{p}(n) = 3}  \\
&amp;amp;\,\hphantom{=} + \cdots
\end{align}\]

&lt;p&gt;Sum up the vertical slices to get&lt;/p&gt;

\[\evt{n} = \prob{T_{p}(n)&amp;gt;0} + \prob{T_{p}(n)&amp;gt;1} + \prob{T_{p}(n)&amp;gt;2} + \cdots\]

&lt;p&gt;Alright. We have to compute $\prob{T_{p}(n) &amp;gt; t}$, the probability that &lt;em&gt;at least one&lt;/em&gt; of the lights has failed to turn off in $t$ seconds (so the ultimate time is more than $t$).
This is just $1 - \prob{T_{p}(n) \leq t}$:  one minus the probability that &lt;em&gt;all&lt;/em&gt; lights turn off within the first $t$ seconds.&lt;/p&gt;

&lt;p&gt;Whether a lightbulb turns off in the first $t$ seconds or not is completely independent of other lightbulbs, so we can just focus on computing $\prob{T_{p}(1) \leq t}$. This value is simply $1 - q^t$, one minus the probability that the lightbulb fails to turn off $t$ times consecutively.&lt;/p&gt;

&lt;p&gt;Overall, we have&lt;/p&gt;

\[\begin{align}
\evt{n} &amp;amp;= \sum_{t=0}^{\infty} \prob{T_{p}(n) &amp;gt; t} \\
&amp;amp;= \sum_{t=0}^{\infty} \left( 1 - \prob{T_{p}(n) \leq t} \right) \\
&amp;amp;= \sum_{t=0}^{\infty} \left( 1 - \left( \prob{T_{p}(1) \leq t}  \right)^n \right) \\
&amp;amp;= \boxed{\sum_{t=0}^{\infty} \left( 1 - \left( 1 - q^t  \right)^n \right)}.
\end{align}\]

&lt;p&gt;We can write this in terms of $p$, our original variable, and get&lt;/p&gt;

\[\boxed{\evt{n} = \sum_{t=0}^{\infty} \left( 1 - \left( 1 - \left( 1-p \right) ^t  \right)^n \right)}.\]

&lt;p&gt;There we go! We’ve got a formula that doesn’t really use any knowledge besides negating probabilities here and there. (Okay, the negations were actually really confusing for me.) No tricky recurrence formula.&lt;/p&gt;

&lt;h3 id=&quot;b-but-infinite-series&quot;&gt;B-but… infinite series!&lt;/h3&gt;

&lt;p&gt;As cool as the new solution is, one issue I have with it is that it is impossible to evaluate exactly as a fraction by hand, due to the infinite sum.&lt;/p&gt;

&lt;p&gt;In code, the best we can do is take a finite number of terms ($t$ up to some amount) or take new terms until the answer stops changing by some amount.&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;itertools&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epsilon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epsilon&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;infinite_series_solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epsilon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;itertools&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;diff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epsilon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;diff&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;
  
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;infinite_series_solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The output is close to what we’ve got from the previous piece of code.&lt;/p&gt;

&lt;p&gt;We can do some nasty algebra to get rid of this infinite sum.&lt;/p&gt;

&lt;p&gt;First, let’s start here; avoid $p$ because it’s nasty:&lt;/p&gt;

\[\evt{n} = \sum_{t=0}^{\infty} \left( 1 - \left( 1 - q ^t  \right)^n \right).\]

&lt;p&gt;We can expand $\left( 1-q^t \right)^n = \left(-q^t+1 \right)^n$ using binomial expansion.&lt;/p&gt;

\[\begin{align}
\evt{n} &amp;amp;= \sum_{t=0}^{\infty} \left( 1 - \left( \sum_{k=0}^n \binom{n}{k} (-1)^k q^{kt} \right) \right) \\
&amp;amp;= \sum_{t=0}^{\infty} \left( 1 - \left(1 + \sum_{k=1}^n \binom{n}{k} (-1)^k q^{kt} \right) \right) \\
&amp;amp;= \sum_{t=0}^{\infty} \left( \sum_{k=1}^n \binom{n}{k} (-1)^{k+1} q^{kt} \right) \\
&amp;amp;= \sum_{k=1}^n \left(  \binom{n}{k} (-1)^{k+1} \left( \sum_{t=0}^{\infty}  q^{kt} \right) \right). \\
\end{align}\]

&lt;p&gt;Using the formula for infinite geometric series, we ultimately get&lt;/p&gt;

\[\boxed{\evt{n} = \sum_{k=1}^n \left(  \binom{n}{k} (-1)^{k+1} \left( \frac{1}{1-q^k} \right)  \right)}.\]

&lt;p&gt;Again, I confirmed that this solution is correct by coding it up. Got the same solution as earlier!&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;binomial_solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;binomial_solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;head-to-head-comparisons&quot;&gt;Head-to-head comparisons&lt;/h2&gt;

&lt;p&gt;Alright, for the purpose of this post, let’s say we have found three solutions. I’ll name them:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;The recurrence formula solution&lt;/li&gt;
  &lt;li&gt;The infinite series solution&lt;/li&gt;
  &lt;li&gt;The binomial solution&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s pit them against each other.&lt;/p&gt;

&lt;h3 id=&quot;time-complexity-analysis&quot;&gt;Time complexity analysis&lt;/h3&gt;

&lt;p&gt;As a computer scientist, I am morally obligated to analyze the &lt;a href=&quot;https://en.wikipedia.org/wiki/Time_complexity&quot;&gt;time&lt;/a&gt; and &lt;a href=&quot;https://en.wikipedia.org/wiki/Space_complexity&quot;&gt;space complexity&lt;/a&gt; of all these solutions.&lt;/p&gt;

&lt;p&gt;For solution #1, as I’ve mentioned earlier, to compute $\evt{n}$ for arbitrary $n$, you unfortunately have to compute it for all smaller values of $n$ too, so the time complexity required to compute an arbitrary $n$ is $\bigo{n^2}$. The good news is, if you &lt;em&gt;do&lt;/em&gt; care about computing it for all $n$ from $0$ up to including or not including $N$, nothing changes, and the time complexity stays at $\bigo{N^2}$.&lt;/p&gt;

&lt;p&gt;Actually, while we’re at it, let’s think about space complexity as well. We definitely need $\bigo{n}$ space to store the answers to the subproblems. In the code I provided above, I also spent $\bigo{n^2}$ space on computing $\binom{n}{k}$ but that’s just me being lazy. I used &lt;a href=&quot;https://docs.python.org/3/library/functools.html#functools.cache&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;functools.cache&lt;/code&gt;&lt;/a&gt;, but you could manually write the function bottom-up, store just the last two rows of the Pascal’s Triangle, and it will work just fine. Overall, the space complexity is $\bigo{n}$.&lt;/p&gt;

&lt;p&gt;For solution #2, we have a bit of an issue because we are computing with infinite series. If we were to take only a constant number of terms, then given arbitrary $n$, the running time is just $\bigo{1}$. (Obviously, for all $n$ up to $N$, the running time is $\bigo{N}$.) The space complexity is just $O(1)$ for the entire computation as well.&lt;/p&gt;

&lt;p&gt;Realistically, we probably want to do something better than just taking a fixed number of terms. We could take a new term if the new term is larger than $\epsilon$, after which we don’t care. That is, we don’t care about $t$ for which&lt;/p&gt;

\[\begin{align}
1 - \left( 1 - q^t \right)^n  &amp;amp;&amp;lt; \epsilon \\
\left( 1 - q^{t} \right)^{n} &amp;amp;&amp;gt; 1 - \epsilon \\
1 - q^{t} &amp;amp;&amp;gt; \left( 1 - \epsilon \right)^{1/n} \\
q^{t} &amp;amp;&amp;lt; 1-\left( 1 - \epsilon \right)^{1/n} \\
t &amp;amp;&amp;gt; \log_{q}\left( 1-\left( 1 - \epsilon \right)^{1/n} \right).
\end{align}\]

&lt;p&gt;We could take new terms as long as the change &lt;em&gt;relative&lt;/em&gt; to the current partial sum is large enough (like in my code). We could do a lot of math to figure out exactly how many terms we need to take to ensure the answer is within $\epsilon$ of the true answer, et cetera.&lt;/p&gt;

&lt;p&gt;The analyses here are a little bit outside of my expertise, so I’m skipping them. I wouldn’t be surprised if you have to take $\mathrm{\Omega}(n)$ terms for the result to be any good. The value of $q$ might also heavily impact the number of terms as well.&lt;/p&gt;

&lt;p&gt;Space complexity is $\bigo{1}$, though. Pretty cool.&lt;/p&gt;

&lt;p&gt;For solution #3, given arbitrary $n$, the time complexity is simply $\bigo{n}$. Space complexity is also $\bigo{1}$. It’s not $\bigo{n}$ because turns out it’s also possible to directly compute $\binom{n}{k}$ from $\binom{n}{k-1}$, &lt;a href=&quot;https://en.wikipedia.org/wiki/Pascal%27s_triangle#Calculating_a_row_or_diagonal_by_itself&quot;&gt;allowing us to scan across any row in the Pascal’s Triangle without extra space&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As with solution #1, to compute the answers for all $n$ up to $N$, the time complexity is $\bigo{N^2}$. I wonder if we could do better than this at all.&lt;/p&gt;

&lt;p&gt;To summarize, solution #2 (infinite series) seems to have the “best” time complexity but that’s probably fake. Solution #3 (binomial) has time complexity of $\bigo{n}$ for arbitrary $n$ and solution #1 (recurrence) has time complexity of $\bigo{n^2}$.&lt;/p&gt;

&lt;h3 id=&quot;numerical-accuracy&quot;&gt;Numerical accuracy&lt;/h3&gt;

&lt;p&gt;It seems like solution #3 (binomial formula) is the best solution, but that’s actually false!&lt;/p&gt;

&lt;p&gt;In the code I provided above, if you try to compute for, say, $n&amp;gt;50$, you’ll find that the answers don’t make sense. At one point, the answers stop being monotonic. At another point, the answers simply cease to follow any pattern at all and just explode to infinity, even at relatively small $n$.&lt;/p&gt;

&lt;p&gt;There’s something about the form of this answer that makes it incredibly not suited to floating point computations. I’m not exactly sure what it is exactly, but it could be the alternating positive/negative signs. (I tried many things, though. I summed up the positive and negative parts separately first; I sorted the terms by $\binom{n}{k}$ size and made sure to sum the smaller terms first; etc.)&lt;/p&gt;

&lt;p&gt;The other two solutions seem to be fine until $n&amp;gt;1000$, where the values in the computation are either too large ($\binom{n}{k}$) or too small ($q^t$) to fit in a floating point number. I can forgive that.&lt;/p&gt;

&lt;p&gt;Overall, solution #1 (recurrence) is better because the formula can be exactly computed. Solution #2 (infinite series) is bound to have some serious floating point errors if you don’t take enough terms.&lt;/p&gt;

&lt;p&gt;Actually… how do we know which solution is the one with the “correct” answer?&lt;/p&gt;

&lt;h3 id=&quot;computing-with-fractions&quot;&gt;Computing with fractions&lt;/h3&gt;

&lt;p&gt;Python is a fun language to use because it comes with so many libraries readily available for explorations like this. Specifically, there is a &lt;a href=&quot;https://docs.python.org/3/library/fractions.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fractions&lt;/code&gt;&lt;/a&gt; library that implements rational number arithmetic for us.&lt;/p&gt;

&lt;p&gt;We could compute the exact fractions with either solution #1 or #3. Suddenly, all numerical problems with solution #3 disappear! After all, we aren’t dealing with floating point shenanigans.&lt;/p&gt;

&lt;p&gt;You can see example results here, for $p=\frac{1}{2}$:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0 0
1 2
2 8/3
3 22/7
4 368/105
5 2470/651
6 7880/1953
7 150266/35433
8 13315424/3011805
9 2350261538/513010785
10 1777792792/376207909
11 340013628538/70008871793
12 203832594062416/40955189998905
13 131294440969788022/25804920098540835
14 822860039794822168/158515937748179415
15 177175812995012739374/33510269239965128331
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We could use this method to compute the “ground truth.” We can convert it to a floating point number and compare it to other solution’s answers to see who’s the closest. As discussed earlier, solution #1 indeed gives the closest numerical value.&lt;/p&gt;

&lt;p&gt;One important thing to note: Using big integers means you can’t just assume all numerical operations are $\bigo{1}$ anymore, so all the time complexity analyses we did earlier are invalidated. If you want to analyze them properly, you’re welcome to. I’m out.&lt;/p&gt;

&lt;h3 id=&quot;summary&quot;&gt;Summary&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Solution&lt;/th&gt;
      &lt;th&gt;Time complexity&lt;/th&gt;
      &lt;th&gt;Space complexity&lt;/th&gt;
      &lt;th&gt;Numerical accuracy&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;#1 (Recurrence)&lt;/td&gt;
      &lt;td&gt;$\bigo{n^2}$&lt;/td&gt;
      &lt;td&gt;$\bigo{n}$&lt;/td&gt;
      &lt;td&gt;Best&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;#2 (Infinite series)&lt;/td&gt;
      &lt;td&gt;fake $\bigo{1}$&lt;/td&gt;
      &lt;td&gt;$\bigo{1}$&lt;/td&gt;
      &lt;td&gt;Acceptable&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;#3 (Binomial formula)&lt;/td&gt;
      &lt;td&gt;$\bigo{n}$&lt;/td&gt;
      &lt;td&gt;$\bigo{1}$&lt;/td&gt;
      &lt;td&gt;Simply broken&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;oeis&quot;&gt;OEIS&lt;/h2&gt;

&lt;p&gt;It turns out we’re not the first to investigate this problem thoroughly. If you take the numerators or the denominators and put them into OEIS, the bible for all the cool integer sequences, you’ll find &lt;a href=&quot;https://oeis.org/A158466&quot;&gt;sequence A158466&lt;/a&gt;, titled “Numerators of $\mathrm{EH}(n)$, the expected value of the height of a probabilistic skip list with $n$ elements and $p=1/2$.”&lt;/p&gt;

&lt;p&gt;One of the comments states:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;$n$ fair coins are flipped in a single toss. Those that show tails are collected and reflipped in another single toss. The process is repeated until all the coins show heads. H(n) is the discrete random variable that denotes the number of tosses required. $\prob{H(n) \leq k} = (1-(1/2)^k)^n$.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is indeed exactly the same problem! But more interesting, however, are the title and this comment elaborating on the title:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;A probabilistic skip list is a data structure for sorted elements with $\bigo{\log n}$ average time complexity for most operations. The probability $p$ is a fixed internal parameter of the skip list.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For some reason, we got ourselves well into computer science territory and stumbled upon &lt;a href=&quot;https://en.wikipedia.org/wiki/Skip_list&quot;&gt;skip lists&lt;/a&gt;! This post is getting long enough as is, so I’m skipping (pun intended) discussing them.&lt;/p&gt;

&lt;p&gt;It seems like no one has found a closed form that’s simpler than any of the solutions we have, though, which is a pity.&lt;/p&gt;

&lt;p&gt;Although, I suppose it makes sense. If you look at &lt;a href=&quot;https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)&quot;&gt;the harmonic series&lt;/a&gt; or &lt;a href=&quot;https://oeis.org/A001008&quot;&gt;the sequence listing A001008&lt;/a&gt;, you’ll find that all formulas are large summations, approximations, or ones that rely on other complicated functions (like &lt;a href=&quot;https://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind&quot;&gt;Stirling numbers of the second kind&lt;/a&gt;). So, even for something as “nice” as the harmonic series, there’s really no way to avoid spending a lot of time on computing the $n$-th number.&lt;/p&gt;

&lt;p&gt;By the way, some extra connections to make:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;This problem is eerily similar to &lt;a href=&quot;https://en.wikipedia.org/wiki/Coupon_collector%27s_problem&quot;&gt;the coupon collector’s problem&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;The alternating plus/minus sums of the binomial coefficients might remind you of &lt;a href=&quot;https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle#Special_case&quot;&gt;the inclusion-exclusion principle&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Did you know you could &lt;a href=&quot;https://en.wikipedia.org/wiki/Pascal%27s_triangle#Construction_as_matrix_exponential&quot;&gt;construct the binomial matrix as a very elegant matrix exponential&lt;/a&gt;? Absolutely wild.&lt;/li&gt;
&lt;/ul&gt;

\[\exp \begin{pmatrix}
\cdot &amp;amp; \cdot &amp;amp; \cdot &amp;amp; \cdot &amp;amp; \cdot \\
1 &amp;amp; \cdot &amp;amp; \cdot &amp;amp; \cdot &amp;amp; \cdot \\
\cdot &amp;amp; 2 &amp;amp; \cdot &amp;amp; \cdot &amp;amp; \cdot \\
\cdot &amp;amp; \cdot &amp;amp; 3 &amp;amp; \cdot &amp;amp; \cdot \\
\cdot &amp;amp; \cdot &amp;amp; \cdot &amp;amp; 4 &amp;amp; \cdot
\end{pmatrix}
=
\begin{pmatrix}
1 &amp;amp; \cdot &amp;amp; \cdot &amp;amp; \cdot &amp;amp; \cdot \\
1 &amp;amp; 1 &amp;amp; \cdot &amp;amp; \cdot &amp;amp; \cdot \\
1 &amp;amp; 2 &amp;amp; 1 &amp;amp; \cdot &amp;amp; \cdot \\
1 &amp;amp; 3 &amp;amp; 3 &amp;amp; 1 &amp;amp; \cdot \\
1 &amp;amp; 4 &amp;amp; 6 &amp;amp; 4 &amp;amp; 1
\end{pmatrix}\]

&lt;p&gt;Alright, I hope you’ve had fun! I’m outta here.&lt;/p&gt;
</description>
        <pubDate>Sun, 11 Aug 2024 19:15:00 -0400</pubDate>
        <link>https://tcpc.me//2024/08/11/decaying-lights.html</link>
        <guid isPermaLink="true">https://tcpc.me//2024/08/11/decaying-lights.html</guid>
        
        <category>Math</category>
        
        
      </item>
    
      <item>
        <title>Review/Summary: How to Design Programs (HTDP vs. SICP)</title>
        <description>&lt;p&gt;This is a summary of &lt;em&gt;How to Design Programs: An Introduction to Programming and Computing&lt;/em&gt;, abbreviated HTDP.&lt;/p&gt;

&lt;p&gt;The book has what I think is an interesting approach to teaching introductory CS. It is different from SICP enough, so I had to read it. (Saying “HTDP is like SICP but easier” is rather misleading for reasons you will see soon.)&lt;/p&gt;

&lt;blockquote class=&quot;infobox no_toc&quot;&gt;
  &lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;
&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#sicp-recap&quot; id=&quot;markdown-toc-sicp-recap&quot;&gt;SICP recap&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#what-do-htdp-authors-have-to-say-about-sicp&quot; id=&quot;markdown-toc-what-do-htdp-authors-have-to-say-about-sicp&quot;&gt;What do HTDP authors have to say about SICP?&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#diving-into-htdp&quot; id=&quot;markdown-toc-diving-into-htdp&quot;&gt;Diving into HTDP&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#part-i-fixed-size-data&quot; id=&quot;markdown-toc-part-i-fixed-size-data&quot;&gt;Part I: Fixed-Size Data&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#the-mechanics&quot; id=&quot;markdown-toc-the-mechanics&quot;&gt;The mechanics&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#the-design-recipe&quot; id=&quot;markdown-toc-the-design-recipe&quot;&gt;The design recipe&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#enumerations-intervals-itemizations&quot; id=&quot;markdown-toc-enumerations-intervals-itemizations&quot;&gt;Enumerations, intervals, itemizations&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#adding-structs&quot; id=&quot;markdown-toc-adding-structs&quot;&gt;Adding structs&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#space-invader-example&quot; id=&quot;markdown-toc-space-invader-example&quot;&gt;Space invader example&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#part-ii-arbitrarily-large-data&quot; id=&quot;markdown-toc-part-ii-arbitrarily-large-data&quot;&gt;Part II: Arbitrarily Large Data&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#structural-recursion&quot; id=&quot;markdown-toc-structural-recursion&quot;&gt;Structural recursion&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#using-a-table-to-figure-out-recursions&quot; id=&quot;markdown-toc-using-a-table-to-figure-out-recursions&quot;&gt;Using a table to figure out recursions&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#recursion-on-a-natural-number&quot; id=&quot;markdown-toc-recursion-on-a-natural-number&quot;&gt;Recursion on a natural number&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#design-by-composition&quot; id=&quot;markdown-toc-design-by-composition&quot;&gt;Design by composition&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#choosing-representations-wisely&quot; id=&quot;markdown-toc-choosing-representations-wisely&quot;&gt;Choosing representations wisely&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#beyond-cons&quot; id=&quot;markdown-toc-beyond-cons&quot;&gt;Beyond &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#part-iii-abstraction&quot; id=&quot;markdown-toc-part-iii-abstraction&quot;&gt;Part III. Abstraction&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#functional-abstraction&quot; id=&quot;markdown-toc-functional-abstraction&quot;&gt;Functional abstraction&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#generic-types&quot; id=&quot;markdown-toc-generic-types&quot;&gt;Generic types&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#how-to-use-abstractions&quot; id=&quot;markdown-toc-how-to-use-abstractions&quot;&gt;How to use abstractions&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#part-iv-intertwined-data&quot; id=&quot;markdown-toc-part-iv-intertwined-data&quot;&gt;Part IV: Intertwined Data&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#extending-part-iis-design-recipe&quot; id=&quot;markdown-toc-extending-part-iis-design-recipe&quot;&gt;Extending part II’s design recipe&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#simultaneous-processing&quot; id=&quot;markdown-toc-simultaneous-processing&quot;&gt;Simultaneous processing&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#part-v-generative-recursion&quot; id=&quot;markdown-toc-part-v-generative-recursion&quot;&gt;Part V: Generative Recursion&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#part-vi-accumulators&quot; id=&quot;markdown-toc-part-vi-accumulators&quot;&gt;Part VI: Accumulators&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;sicp-recap&quot;&gt;SICP recap&lt;/h2&gt;

&lt;p&gt;First, let’s do a quick walkthrough of the classic: &lt;em&gt;Structure and Interpretation of Computer Programs&lt;/em&gt;, abbreviated SICP. Well… just the first two chapters really. This is to provide a reference for comparing HTDP to SICP. You can skip this section if you are already familiar with SICP. I also previously wrote &lt;a href=&quot;/2021/06/24/sicp-is-cool.html&quot;&gt;a post on some cool things I learned from SICP&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The book can be accessed &lt;a href=&quot;https://mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/6515/sicp.zip/index.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The book was used as a textbook for an old version of MIT’s introductory programming class. Its approach is unusual by today’s standards. For one, it uses the Scheme programming language. It uses a relatively “functional” style of programming rather than the “imperative” style seen in most courses. It is also quite theoretical.&lt;/p&gt;

&lt;p&gt;The book starts by discussing basic Scheme syntax, like defining constants, applying procedures, and using conditionals (§1.1). All functions at this point are pure&lt;sup id=&quot;fnref:pure&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:pure&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;, the book uses the substitution model, at least for the time being. This section uses approximating a square root as its main example.&lt;/p&gt;

&lt;p&gt;We then investigate the shapes of the processes generated by similar-looking procedures (§1.2). Specifically, the book distinguishes recursion from iteration, more commonly known as tail recursion in other programming languages&lt;sup id=&quot;fnref:noloop&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:noloop&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;, using classic examples like factorial and Peano arithmetic addition. It also touches on recursion with exponential time and has a cool exercise on binary exponentiation.&lt;/p&gt;

&lt;p&gt;Afterward, the book discusses higher-order functions as a way to abstract similar processes (§1.3). Higher-order functions are also used to make the meaning of some procedures clearer.&lt;/p&gt;

&lt;p&gt;Concluding the first chapter, SICP introduces the idea of data abstraction (§2.1), which imposes a guideline: a user should be able to manipulate compound data objects without making any assumptions about the internal representation of the data. To this end, interfaces to abstract data types are exposed as procedures called constructors and selectors. The idea of the “abstraction barrier” is introduced. The book uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons&lt;/code&gt; as its primitive for building compound data and shows how to build a rational number with it. With the abstraction, it is possible to change the internal representation of a rational number type so it simplifies the fraction when the numerator or the denominator is being extracted rather than when the fraction is created. It also has cool exercises on representing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons&lt;/code&gt; and natural numbers via Church encoding.&lt;/p&gt;

&lt;p&gt;The next section (§2.2) goes into more complicated data structures like linked lists and trees. It also introduces conventional list manipulation functions like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt;. The exercises go as far as asking the reader to solve the N Queen problem.&lt;/p&gt;

&lt;p&gt;The rest of chapter 2 walks through complex examples like symbolic differentiation and Huffman encoding trees (§2.3). It explains how to implement a system that provides OOP and polymorphism-like features (§2.4-2.5).&lt;/p&gt;

&lt;p&gt;The rest of the chapters deal with mutation and environmental model (§3.1-3.3), concurrency (§3.4), stream processing (§3.5), interpreter (§4.1), alternative language features like lazy evaluation and logic programming (§4.2-4.4), a model of register machines with an assembly-like language (§5.1-5.3), and compilation (§5.4-5.5). These topics, except mutations, are rather advanced by most “intro to CS” course standards. For this review, I’m not focusing on these because HTDP does not cover them. (Also, I’m just not as interested in teaching them as teaching the more basic content.)&lt;/p&gt;

&lt;p&gt;One thing to note is that SICP sprinkles math applications and algorithms throughout, so students should be exposed to a wide range of canonical examples in computer science (e.g. binary search, trees).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;what-do-htdp-authors-have-to-say-about-sicp&quot;&gt;What do HTDP authors have to say about SICP?&lt;/h2&gt;

&lt;p&gt;The authors of HTDP published a paper titled &lt;em&gt;The Structure and Interpretation of the Computer Science Curriculum&lt;/em&gt;, accessible &lt;a href=&quot;https://www2.ccs.neu.edu/racket/pubs/jfp2004-fffk.pdf&quot;&gt;here&lt;/a&gt;. The paper puts forth a critique of SICP and explains, at a high level, how HTDP’s approach fixes SICP’s shortcomings.&lt;/p&gt;

&lt;p&gt;A major problem with SICP is described pretty well there so let me just quote it.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;While the course briefly explains programming as the definition of some recursive procedures, it does not discuss how programmers determine which procedures are needed or how to organize these procedures. While it explains that programs benefit from functions as first-class values, it does not show how programmers discover the need for this power. While SICP introduces the idea that programs should use abstraction layers, it never mentions how or when programmers should introduce such layers of abstraction. […]&lt;/p&gt;

  &lt;p&gt;More generally, SICP doesn’t state how to program and how to manage the design of a program. It leaves these things implicit and implies that students can discover a discipline of design and programming on their own. The course presents the various uses and roles of programming ideas with a series of examples. Some exercises then ask students to modify this code basis, requiring students to read and study code; others ask them to solve similar problems, which means they have to study the construction and to change it to the best of their abilities. In short, SICP students learn by copying and modifying code, which is barely an improvement over typical programming text books.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;HTDP solves this issue by adopting a different approach.&lt;/p&gt;

&lt;p&gt;First, its explicit goal is to teach the necessary skills for students to succeed in computer science. These include understanding problem statements and mapping them, systematically, to code. HTDP also requires good habits like testing.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[S]tudents must learn that programming requires far more than writing down code and running it on some haphazardly chosen examples afterwards.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Second, it provides a detailed “design recipe” that students can follow to design their programs rather than relying on ad-hoc insights as SICP does. The design recipe and the templates are driven by the structure of data given by the problems. Language constructs are introduced as needed to represent increasingly complex information.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“[W]hen we teach how to program, we should let data drive the syllabus.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;HTDP also addresses the issue with SICP’s dependence on mathematical and engineering examples, which can be intimidating for students as they cannot distinguish actual programming knowledge from domain knowledge, by avoiding those topics and explicitly indicating if something is domain knowledge. HTDP teaches how to write interactive programs with graphical interfaces (“world programs”), which is more exciting to students.&lt;/p&gt;

&lt;p&gt;The paper did not mention this, but it is important to note: HTDP requires students to work with resources like DrRacket stepper (somewhat like a debugger; for expanding expressions mechanistically) and library references/documentation. The exercises practice these skills. I think this is pretty nice.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;diving-into-htdp&quot;&gt;Diving into HTDP&lt;/h2&gt;

&lt;p&gt;The book’s prologue is a whirlwind tour on how one could write a program that animates a static image of a rocket, landing. The introduction uses an informal tone and concludes that despite having seen all of this, the reader should not consider themselves a programmer yet. What one really needs to learn in programming is how to &lt;em&gt;design&lt;/em&gt; programs, not just how to hack together pieces of code to make something work.&lt;/p&gt;

&lt;p&gt;The main chunk of the book is organized by the complexity of data in the sample problems throughout the book. Of all six parts of the book, each part refines the design recipe and the corresponding templates, introducing new control constructs whenever natural. The parts go like this:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Fixed-Size Data&lt;/li&gt;
  &lt;li&gt;Arbitrarily Large Data&lt;/li&gt;
  &lt;li&gt;Abstraction&lt;/li&gt;
  &lt;li&gt;Intertwined Data&lt;/li&gt;
  &lt;li&gt;Generative Recursion&lt;/li&gt;
  &lt;li&gt;Accumulators
Between each part of the book, there is an “intermezzo” that dives into nitty-gritty details like Racket’s quoting mechanism, scoping, and exact/inexact number representations. The first of these intermezzos describes the syntax and semantics of Racket&lt;sup id=&quot;fnref:subset&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:subset&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; relatively formally. The semantics is based on substitution rather than closures and environments.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The rest of this blog post is a walkthrough of HTDP’s contents. I spend the most time describing the first two parts, specifically the design recipe and the templates, as these are the novel, pedagogical ideas presented by this book. The last four parts are skimmed over.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;part-i-fixed-size-data&quot;&gt;Part I: Fixed-Size Data&lt;/h3&gt;

&lt;h4 id=&quot;the-mechanics&quot;&gt;The mechanics&lt;/h4&gt;

&lt;p&gt;The first chapter describes the built-in data types: numbers, strings, images, and booleans. It shows how to define constants and apply built-in functions. Images are manipulated via the book’s library (or “teachpack”) &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2htdp/image&lt;/code&gt; which has primitive operations like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;circle&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;line&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image-width&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;place-image&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;overlay&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;beside&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;above&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;save-image&lt;/code&gt;, etc. The chapter also discusses predicates for verifying data types and properties.&lt;/p&gt;

&lt;p&gt;The second chapter teaches function definition and application in detail, walking through a series of substitutions/reductions on simple examples. It illustrates a sample batch program that manipulates text files and a graphical, interactive program (“world program”) using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2htdp/universe&lt;/code&gt; teachpack. At a high level, world programs are structured as state machines. The reader needs to supply an initial state, a state transition function, a function for rendering a state, and functions to handle events like clicks/keystrokes. The first world program looks something like this.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;BACKGROUND&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty-scene&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;DOT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;circle&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;solid&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;red&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
 
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;big-bang&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;on-tick&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sub1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stop-when&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;zero?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to-draw&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;place-dot-at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;on-key&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
 
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;place-dot-at&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;place-image&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;DOT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;BACKGROUND&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
 
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stop&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;the-design-recipe&quot;&gt;The design recipe&lt;/h4&gt;

&lt;p&gt;We finally get into the meat in chapter 3 as it introduces the design recipe and provides plenty of drills for the reader to practice. First, chapter 3 discusses the design recipe for designing &lt;em&gt;a function&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Step 1 asks the reader to identify the information given in the problem. The reader should decide how to &lt;em&gt;represent&lt;/em&gt; the information using data and write down how to &lt;em&gt;interpret&lt;/em&gt; the data as information. The book calls this “data definition.” An example would be something like this:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; A Temperature is a Number. &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; interpretation: represents Celsius degrees&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Step 2 is writing down a signature, a statement of purpose, and a function header. The purpose statement should let people reading the program understand what the function does without having to look at the implementation. The purpose statement may refer to parameter names.&lt;/p&gt;

&lt;p&gt;Step 3 is writing some functional examples. These could be written as comments, or as taught in the later sections, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check-expect&lt;/code&gt; statements.&lt;/p&gt;

&lt;p&gt;Step 4, which is the most interesting suggestion in this book, is to write a template that includes all the givens. At this point in the process of designing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;area-of-square&lt;/code&gt;, students should have code that looks like this:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; Number -&amp;gt; Number&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; computes the area of a square with side len &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; given: 2, expect: 4&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; given: 7, expect: 49&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;area-of-square&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(... len ...)&lt;/code&gt; in the function body tells us that the answer should be some function of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;len&lt;/code&gt;. The template seems unexciting right now, but as the book introduces more complex data, the template becomes an extremely useful pedagogical tool. We’ll see this soon.&lt;/p&gt;

&lt;p&gt;Step 5 is to fill in the body, then finally, the last step is to test and fix the function.&lt;/p&gt;

&lt;p&gt;This design recipe is to be applied to all functions. World programs usually require defining the “world state” data type and designing multiple functions required by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;big-bang&lt;/code&gt;, like a function to be executed on every clock cycle. The world state should be based on properties that change over time. The rest can be constants.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please take a look at &lt;a href=&quot;https://github.com/emaphis/HtDP2e-solutions/blob/master/1-Fixed-Size-Data/03-HtDP/ex044.rkt&quot;&gt;the code here&lt;/a&gt;&lt;/strong&gt;. This is what a complete program should ideally look like, according to the book. The problem here is to animate a car moving to the right. There should be a stationary tree. Also, if the user clicks somewhere on the canvas, the car should teleport there. In the code, observe that:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WorldState&lt;/code&gt; is carefully defined.&lt;/li&gt;
  &lt;li&gt;All functions, including &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tock&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;render&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end?&lt;/code&gt; are designed and tested. The examples for functions with graphical inputs/outputs may need to be determined via experimentation in the REPL.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The book emphasizes separating the “model” from the “view.” It has exercises that explore alternative state definitions. For example, if the state is defined as “the number of clock ticks since the program starts,” it is possible to animate the car moving to the right but more substantially difficult to teleport the car on user clicks.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;enumerations-intervals-itemizations&quot;&gt;Enumerations, intervals, itemizations&lt;/h4&gt;

&lt;p&gt;The rest of part 1 (chapters 4-7) dive into information that cannot be represented with just the built-in data types introduced so far. It discusses a very mechanistic way of dealing with different kinds of data.&lt;/p&gt;

&lt;p&gt;An enumeration defines a collection of data as a finite number of pieces of data, each spelled out explicitly. For example,&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; A TrafficLight is one of the following Strings:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – &quot;red&quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – &quot;green&quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – &quot;yellow&quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; interpretation: the three strings represent the three &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; possible states that a traffic light may assume &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Whenever one designs a function taking an enumeration as an input, they should use a conditional template that covers all exactly the cases as defined in the enumeration.&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; TrafficLight -&amp;gt; TrafficLight&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; yields the next state given current state s&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;check-expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;traffic-light-next&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;red&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;green&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;traffic-light-next&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;string=?&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;red&quot;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;string=?&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;green&quot;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;string=?&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;yellow&quot;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;An interval defines classes of numbers via boundaries. The data definition and the corresponding template would look something like this:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; A WorldState is a Number.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; interpretation: number of pixels between the top and the UFO&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; A WorldState falls into one of three intervals: &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – between 0 and CLOSE&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – between CLOSE and HEIGHT&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – below HEIGHT&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; WorldState -&amp;gt; WorldState&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;CLOSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;CLOSE&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;HEIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;HEIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Itemization is a way of including elements from both finite and infinite classes. It describes a data type as the sum of all the subclasses. See this in the next section on the space invader game.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;adding-structs&quot;&gt;Adding structs&lt;/h4&gt;

&lt;p&gt;The design recipe mandates that the user thinks of and writes functional examples for all subclasses of the input (and covers the boundary cases in case of intervals). It is also possible to divide the function into multiple functions, one per subclass. The conditional statement in the top-level function would act as a dispatcher.&lt;/p&gt;

&lt;p&gt;The book also introduces structs in Racket. Structs can be defined via the syntax &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(define-struct structname [field1 field2 ...])&lt;/code&gt; like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(define-struct posn [x y])&lt;/code&gt; for example. This would automatically generate a constructor (named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make-structname&lt;/code&gt;) and selector procedures (named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;structname-fieldk&lt;/code&gt;). It would also generate a predicate for checking if an object belongs to this struct type (named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;structname?&lt;/code&gt;). For the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;posn&lt;/code&gt; example above, we would get the functions &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make-posn&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;posn-x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;posn-y&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;posn?&lt;/code&gt; with the following properties:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;∀ x y,  (posn-x (make-posn x y)) = x
∀ x y,  (posn-y (make-posn x y)) = y
∀ z,    (∃ x y, z = (make-posn x y)) ↔ (posn? z) = true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The programmer should create data examples to ensure that the data definition is robust enough to represent the information needed for the problem.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; An R3 is a structure:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;   (make-r3 Number Number Number)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;define-struct&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;r3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
 
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ex1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;make-r3&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ex2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;make-r3&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;-1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When a function takes in a struct as input, its template should contain all selector expressions to remind us that those are the data given to us.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; R3 -&amp;gt; Number &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; determines the distance of p to the origin &lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;r3-distance-to-0&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;r3-x&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;r3-y&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;r3-z&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;space-invader-example&quot;&gt;Space invader example&lt;/h4&gt;

&lt;p&gt;The demonstrating example in the book is creating a space invader game: A UFO spawns at a random horizontal position and descends from the top of the screen. A tank at the bottom of the screen can be moved left or right by the player. The user can press the space bar to “shoot” one bullet. A natural data definition of the game state would have two cases:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; A SIGS (short for Space Invader Game State) is one of: &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – (make-aim UFO Tank)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – (make-fired UFO Tank Missile)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; interpretation: represents the complete state of a  space invader game&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;define-struct&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;aim&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ufo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;tank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;define-struct&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;fired&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ufo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;tank&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;missile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UFO&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Tank&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Missile&lt;/code&gt; are defined further like this.&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; A UFO is a Posn. &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; interpretation: (make-posn x y) is the UFO&apos;s location &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; (using the top-down, left-to-right convention)&lt;/span&gt;
 
&lt;span class=&quot;c1&quot;&gt;; A Tank is a structure:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;   (make-tank Number Number). &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; interpretation: (make-tank x dx) specifies the position:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; (x, HEIGHT) and the tank&apos;s speed: dx pixels/tick&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;define-struct&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;tank&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;loc&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;vel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
 
&lt;span class=&quot;c1&quot;&gt;; A Missile is a Posn. &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; interpretation (make-posn x y) is the missile&apos;s place&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This means the rendering function should have a template that looks like&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;si-render&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;aim?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;aim-tank&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;aim-ufo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fired?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fired-tank&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fired-ufo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fired-missle&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;See &lt;a href=&quot;https://github.com/emaphis/HtDP2e-solutions/blob/master/1-Fixed-Size-Data/06-Itemizations-Structures/ex100.rkt#L145-L188&quot;&gt;the code here&lt;/a&gt; for the implementation.&lt;/strong&gt; Note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;si-render&lt;/code&gt; makes calls to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tank-render&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ufo-render&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;missile-render&lt;/code&gt; helper functions, which are also developed using the design recipe as well.&lt;/p&gt;

&lt;p&gt;At this point, it should be clear to any functional programmers that we are creating algebraic data types, though recursion is currently out of the picture. This functional approach makes it very natural for students to turn any given problem statement into suitable data definitions. It’s also quite aesthetically pleasing.&lt;/p&gt;

&lt;p&gt;There are a few things to note. First, the book suggests that “[i]f a function deals with nested structures, develop one function per level of nesting.”&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;;; Do this:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; UFO -&amp;gt; UFO&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ufo-move-1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ufo-loc&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ufo-vel&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; Posn Vel -&amp;gt; Posn &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; adds v to p &lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;posn+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Don&apos;t do this:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; UFO -&amp;gt; UFO&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ufo-move-1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;posn-x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ufo-loc&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
   &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;posn-y&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ufo-loc&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
   &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;vel-deltax&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ufo-vel&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
   &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;vel-deltay&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ufo-vel&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Second, sometimes a function takes multiple arguments, not all of which need to be deconstructed. In that case, treat those arguments as atomic data.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href=&quot;https://dl.acm.org/doi/10.1145/2692915.2628137&quot;&gt;On Teaching How to Design Programs: Observations from a Newcomer&lt;/a&gt;&lt;/em&gt; discusses common issues that show up in students’ code. Two of which are “stubborn sum” and “stubborn structure,” where an itemization or a struct is deconstructed (via conditionals and selectors), but the entire object is also passed to the helper functions, requiring the object to be deconstructed over and over (see pg.6).&lt;/p&gt;

&lt;p&gt;When developing a class, we should ensure that the design recipe is followed exactly. The templates should be checked by instructors, and ideally, the students should learn to recognize code that deviates from the template as well.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;part-ii-arbitrarily-large-data&quot;&gt;Part II: Arbitrarily Large Data&lt;/h3&gt;

&lt;h4 id=&quot;structural-recursion&quot;&gt;Structural recursion&lt;/h4&gt;

&lt;p&gt;The second part of the book deals with recursive data types. It introduces a list with the following data definition:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; A List-of-strings is one of: &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – &apos;()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – (cons String List-of-strings)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(first (cons x y)) = x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(rest (cons x y)) = y&lt;/code&gt;. (We could have created our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pair&lt;/code&gt; struct instead if we wanted but it is good to use existing built-ins.)&lt;/p&gt;

&lt;p&gt;Though the definition is recursive, the book shows that the same design recipe applies, with slight modifications. For the template, we note that there must be a conditional statement distinguishing between each case in the data definition, as usual. The body of each case should have appropriate selector expressions. For recursive cases, it is likely that the recurring structure is used as an argument to the function we are writing.&lt;/p&gt;

&lt;p&gt;See this example. To write a function that finds the length of a list of strings, we need to distinguish between the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;()&lt;/code&gt; case and the recursive case. For the recursive case, we note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(first alos)&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(rest alos)&lt;/code&gt; might appear in the answer. Since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(rest alos)&lt;/code&gt; is also a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List-of-strings&lt;/code&gt;, we note that we will use the answer from the recursive call, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(how-many (rest alos))&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; List-of-strings -&amp;gt; Number&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; determines how many strings are on alos&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;how-many&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;;or `else`&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;how-many&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If the input is a complex itemization, it might be good to write helper functions specialized to all cases.&lt;/p&gt;

&lt;p&gt;The programmer should &lt;em&gt;not&lt;/em&gt; attempt to expand &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(how-many (rest alos))&lt;/code&gt; when working through an example as that would be confusing. Instead, the programmer should rely on the purpose statement to figure out the result of that expression.&lt;/p&gt;

&lt;p&gt;Note that this style of recursion is called “structural recursion,” and not all problems can be solved with this. This is, however, the most natural and common form of recursion, and I think the book made the right decision to focus on this.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;using-a-table-to-figure-out-recursions&quot;&gt;Using a table to figure out recursions&lt;/h4&gt;

&lt;p&gt;The book suggests a method for solving recursive problems. First, come up with an example input. Then, create a table where the columns are: the input, the fields of the input (via selector expressions), the expected answer for recursive sub-inputs, and the expected answer for this input. This table should contain a row with the example input and as many rows as needed for all sub-inputs.&lt;/p&gt;

&lt;p&gt;For example, consider the list &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cons &quot;x&quot; (cons &quot;b&quot; (cons &quot;a&quot; &apos;())))&lt;/code&gt;. The table would look like this.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alos&lt;/code&gt;&lt;/th&gt;
      &lt;th&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(first alos)&lt;/code&gt;&lt;/th&gt;
      &lt;th&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(rest alos)&lt;/code&gt;&lt;/th&gt;
      &lt;th&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(how-many (rest alos))&lt;/code&gt;&lt;/th&gt;
      &lt;th&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(how-many alos)&lt;/code&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cons &quot;a&quot; &apos;())&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;a&quot;&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cons &quot;b&quot; (cons &quot;a&quot; &apos;()))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;b&quot;&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cons &quot;a&quot; &apos;())&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cons &quot;x&quot; (cons &quot;b&quot; (cons &quot;a&quot; &apos;())))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;x&quot;&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cons &quot;b&quot; (cons &quot;a&quot; &apos;()))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;From this table, we can conclude that the answer in the recursive case is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(+ 1 (how-many (rest alos)))&lt;/code&gt;. (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(first alos)&lt;/code&gt; turns out to be unnecessary here.)&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;recursion-on-a-natural-number&quot;&gt;Recursion on a natural number&lt;/h4&gt;

&lt;p&gt;This part of the book also defines a natural number using Peano’s construction.&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; An N is one of: &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – 0&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – (add1 N)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; interpretation: represents the counting numbers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;One could think of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; as analogous to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;()&lt;/code&gt;, positive numbers &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt; as analogous to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons&lt;/code&gt; cells, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sub1&lt;/code&gt; acts as a selector analogous to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rest&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This makes it possible to perform recursion on a natural number while still following the design recipe. For example, this function turns a string into a list of this string repeated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; times.&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; N String -&amp;gt; List-of-strings &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; creates a list of n copies of s&lt;/span&gt;
 
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;check-expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;copier&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;check-expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;copier&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
              &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cons&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;hello&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cons&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;hello&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())))&lt;/span&gt;
 
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;copier&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;zero?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;positive?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cons&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;copier&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sub1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;design-by-composition&quot;&gt;Design by composition&lt;/h4&gt;

&lt;p&gt;Sometimes, while designing a function, we must design an auxiliary function using the same design recipe as well. For example, when designing a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sort&lt;/code&gt; function using structural recursion, we get a template that looks like this.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; List-of-numbers -&amp;gt; List-of-numbers &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; rearranges alon in descending order&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;check-expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sort&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;check-expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sort&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;check-expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sort&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;check-expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sort&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;-5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
              &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;-5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sort&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
           &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sort&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We must assume that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(sort&amp;gt; (rest alon))&lt;/code&gt; already gives us a sorted version of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(rest alon)&lt;/code&gt;. We have to combine this with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(first alon)&lt;/code&gt; somehow to get the final answer. This suggests that we need &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; Number List-of-numbers -&amp;gt; List-of-numbers&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; inserts n into the sorted list of numbers alon&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;insert&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This allows us to fill in the implementation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sort&amp;gt;&lt;/code&gt; like this.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sort&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;insert&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sort&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;alon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of course, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert&lt;/code&gt; will be designed using structural recursion on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alon&lt;/code&gt; as well.&lt;/p&gt;

&lt;p&gt;The book also walks through another example where it is sometimes necessary to generalize the function arguments so we have enough data to work with when performing structural recursion.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;choosing-representations-wisely&quot;&gt;Choosing representations wisely&lt;/h4&gt;

&lt;p&gt;There is some amount of creativity involved in choosing data representations. For example, suppose we want to design a function that takes in a non-empty list of temperatures. We could use the standard definition of lists and requires additionally as a precondition that the list is not empty, but it is not quite clear how the template should look like.&lt;/p&gt;

&lt;p&gt;Here is an alternative representation where empty lists are simply not representable:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; An NEList-of-temperatures is one of: &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – (cons CTemperature &apos;())&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – (cons CTemperature NEList-of-temperatures)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; interpretation: non-empty lists of Celsius temperatures &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The template will look like this.&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; NEList-of-temperatures -&amp;gt; Number&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; computes the sum of the given temperatures&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; tests omitted&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ne-l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ne-l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ne-l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ne-l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ne-l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Note how we use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(empty? (rest ne-l))&lt;/code&gt; to figure out whether we are in the base case or the recursive case. For each case, we write down the appropriate selector expressions. Specifically, for the base case, it only makes sense to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(first ne-l)&lt;/code&gt;. For the recursive case, we use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(first ne-l)&lt;/code&gt; and recurses on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(rest ne-l)&lt;/code&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;beyond-cons&quot;&gt;Beyond &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;Of course, we should remember that it is possible to define algebraic data types with structs rather than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons&lt;/code&gt; as well. One example in the book involves representing Russian dolls.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; An RD (short for Russian doll) is one of: &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – String &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – (make-layer String RD)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;define-struct&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;layer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;doll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; a data example satisfying the data definition for RD&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ex-rd&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;make-layer&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;pink&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;make-layer&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;black&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;white&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The template would similarly use the appropriate selectors. Specifically, for the recursive case, we have access to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(layer-color an-rd)&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(layer-doll an-rd)&lt;/code&gt;. We likely apply the function recursively, so we write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(depth (layer-doll an-rd))&lt;/code&gt; in the template.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; RD -&amp;gt; Number&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; how many dolls are a part of an-rd &lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;depth&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;an-rd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;string?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;an-rd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;layer?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;an-rd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;layer-color&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;an-rd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;depth&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;layer-doll&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;an-rd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The rest of part 2 is just variations on these ideas, e.g. itemizations with lists, lists with structs, and recursive structs with lists.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;part-iii-abstraction&quot;&gt;Part III. Abstraction&lt;/h3&gt;

&lt;h4 id=&quot;functional-abstraction&quot;&gt;Functional abstraction&lt;/h4&gt;

&lt;p&gt;Having covered both fixed-size data and arbitrarily large data, the book takes a detour and discusses functional abstraction. This is obviously different from SICP, which starts with functional abstraction followed by data abstraction later.&lt;/p&gt;

&lt;p&gt;First, part III shows how one might see similarities in the implementations of two functions and extract them by abstracting the similarities. This results in functions that take in other functions as arguments. A canonical example is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sort&lt;/code&gt;, which takes in a comparison function.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;generic-types&quot;&gt;Generic types&lt;/h4&gt;

&lt;p&gt;We also discuss how the function signature, i.e. the type of the function, can be abstracted. For example, we can define parameterized list types as follows:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; A [List-of ITEM] is one of: &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – &apos;() &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – (cons ITEM [List-of ITEM])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;how-to-use-abstractions&quot;&gt;How to use abstractions&lt;/h4&gt;

&lt;p&gt;The book then changes its focus from &lt;em&gt;creating&lt;/em&gt; abstractions to &lt;em&gt;using&lt;/em&gt; abstractions. It discusses conventional, higher-order functions like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sort&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;andmap&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ormap&lt;/code&gt;, and introduces the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local&lt;/code&gt; special form which allows creating a local scope. This is used to create functions/predicates that are fed into those well-known abstractions. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lambda&lt;/code&gt; form is also introduced a bit after this.&lt;/p&gt;

&lt;p&gt;As with previous chapters, HTDP provides a design recipe for using abstractions. We follow the same design recipe, but the details are slightly different. The first three steps involve defining the data, writing the purpose statement for the function, and creating examples. For this example, we try to create an image consisting of the given dots. The first three steps yield this:&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; [List-of Posn] -&amp;gt; Image &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; adds the Posns on lop (short for list of positions) to the empty scene &lt;/span&gt;
 
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;check-expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dots&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;make-posn&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
              &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;place-image&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;DOT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;31&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;EMPTY-SCENE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
 
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dots&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;EMPTY-SCENE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;; dummy result&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The fourth step is to write a template. To do this, we must choose an abstraction that is best suited to the problem. This can be done by observing the function signature and seeing if we can potentially plug in our data types and make things work according to the purpose statement of the candidate abstraction. In this example, good candidates are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foldr&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foldl&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; foldr : {X Y} [X Y -&amp;gt; Y] Y [List-of X] -&amp;gt; Y&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; foldl : {X Y} [X Y -&amp;gt; Y] Y [List-of X] -&amp;gt; Y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It seems that we could substitute &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Posn&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Y&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt;. This means the abstraction will take&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;A function of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Posn Image -&amp;gt; Image&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;A starting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and it will output an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since we want to start with an empty image and gradually add dots to the image, the template thus looks like this:&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dots&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;; Posn Image -&amp;gt; Image &lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;add-one-dot&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;scene&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;posn-x&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;posn-y&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;scene&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foldr&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;add-one-dot&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;EMPTY-SCENE&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The fifth and sixth steps are as usual: fill in the body and test the function. This is the final result.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dots&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;; Posn Image -&amp;gt; Image &lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;; adds a DOT at p to scene&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;add-one-dot&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;scene&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;place-image&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;DOT&lt;/span&gt;
                         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;posn-x&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;posn-y&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                         &lt;span class=&quot;nv&quot;&gt;scene&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foldr&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;add-one-dot&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;EMPTY-SCENE&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The last chapter of this part introduces the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lambda&lt;/code&gt; form, allowing us to avoid the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local&lt;/code&gt; form and shorten the code into:&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dots&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foldr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;scene&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
           &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;place-image&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;DOT&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;posn-x&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;posn-y&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;nv&quot;&gt;scene&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
         &lt;span class=&quot;nv&quot;&gt;EMPTY-SCENE&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;part-iv-intertwined-data&quot;&gt;Part IV: Intertwined Data&lt;/h3&gt;

&lt;h4 id=&quot;extending-part-iis-design-recipe&quot;&gt;Extending part II’s design recipe&lt;/h4&gt;

&lt;p&gt;This part is simply an extension of Part II. The data definitions here contain mutual recursion. The design recipe thus requires the reader to design multiple functions, specialized to each definition, at the same time.&lt;/p&gt;

&lt;p&gt;An example used in this part is representing S-expressions, similar to Racket’s.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; An S-expr is one of: &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – Atom&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – SL&lt;/span&gt;
 
&lt;span class=&quot;c1&quot;&gt;; An SL (short for S list) is one of: &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – &apos;()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – (cons S-expr SL)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; An Atom is one of: &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – Number&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – String&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; – Symbol &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Suppose we want to create a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt; that counts the occurrences of symbol &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sy&lt;/code&gt; in a given S-expression &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sexp&lt;/code&gt;. We should create three functions, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count-atom&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count-sl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt; acts as a dispatcher, so the template (which is just the full implementation) is easy to fill.&lt;/p&gt;

&lt;p&gt;The other two are specialized to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SL&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Atom&lt;/code&gt; and actually do the work. These have to contain the appropriate selector expressions. Specifically, for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count-sl&lt;/code&gt;, in the non-empty case, we need to have access to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(first sl)&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(rest sl)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(first sl)&lt;/code&gt; is of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S-expr&lt;/code&gt; and we need to count the symbol occurences in it, we write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(count (first sl) sy)&lt;/code&gt; in our template. Since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(rest sl)&lt;/code&gt; is of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SL&lt;/code&gt;, we can write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(count-sl (rest sl) sy)&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sexp&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;atom?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sexp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
 
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count-sl&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sl&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count-sl&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]))&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count-atom&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;number?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;string?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;symbol?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Filling in the functions involves the same process as in earlier parts. We get the following code.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; S-expr Symbol -&amp;gt; N &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; counts all occurrences of sy in sexp &lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sexp&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;atom?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sexp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count-atom&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sexp&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count-sl&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sexp&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]))&lt;/span&gt;
 
&lt;span class=&quot;c1&quot;&gt;; SL Symbol -&amp;gt; N &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; counts all occurrences of sy in sl &lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count-sl&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sl&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count-sl&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]))&lt;/span&gt;
 
&lt;span class=&quot;c1&quot;&gt;; Atom Symbol -&amp;gt; N &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; counts all occurrences of sy in at &lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count-atom&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;number?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;string?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;symbol?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;symbol=?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because the process is rather mechanical, students should be able to arrive at this solution if they follow the design recipe carefully. It would have been difficult to ask students to design something like this if we did not provide an overarching pattern that can be internalized and generalized.&lt;/p&gt;

&lt;p&gt;The chapter also remarks that after doing all this work, we can simplify the code in multiple steps:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count-sl&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count-atom&lt;/code&gt; can be made local to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SL&lt;/code&gt; is just a list, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count-sl&lt;/code&gt; can be implemented with existing abstractions, in this case, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foldl&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count-atom&lt;/code&gt; can also be simplified a bit.&lt;/li&gt;
  &lt;li&gt;Finally, we can inline &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count-sl&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count-atom&lt;/code&gt; so there is no need for local scopes. Although, in this case, it might be better not to inline &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count-atom&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The next few chapters in part III are just a lot of exercises/projects, including writing an interpreter for a subset of Racket using the newly introduced environmental model.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;simultaneous-processing&quot;&gt;Simultaneous processing&lt;/h4&gt;

&lt;p&gt;This chapter explicitly considers functions that take in two arguments or more, both of which are structurally recursive. There are three cases to consider.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 1: Treat the other argument as atomic.&lt;/strong&gt; We have seen this previously in the book. Reading the purpose statement and functional examples, the programmer should determine which variable to recurse on. Consider this example:&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; [List-of Number] [List-of Number] -&amp;gt; [List-of Number]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; replaces the final &apos;() in front with end&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;replace-eol-with&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;front&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;front&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The purpose statement suggests that we need to recurse on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;front&lt;/code&gt;, while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end&lt;/code&gt; can be treated atomically. Therefore, the corresponding template only has conditionals and selectors on the first argument. (This function is analogous to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;append&lt;/code&gt; or list concatenation.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 2: The arguments are of equal length.&lt;/strong&gt; A function may take in two lists with equal length and perform an operation that pairs up the list elements coordinate-wise. In this case, the template’s conditional can act on either argument. The selector expressions should select from both arguments. Make sure to call the function recursively on smaller inputs.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; [List-of Number] [List-of Number] -&amp;gt; [List-of Number]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; multiplies the corresponding items on hours and wages/h &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; assume the two lists are of equal length &lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;wages*&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;hours&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;wages/h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;wages/h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;wages*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;v2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;hours&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;wages/h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Case 3: The arguments are independent.&lt;/strong&gt; In this case, we write down the cases for each argument, then find the Cartesian product. We fill in the template, just like we have always done, for all cases.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; [List-of Symbol] N -&amp;gt; Symbol&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; extracts the nth symbol from l; &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; signals an error if there is no such symbol&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;list-pick&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
     &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sub1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sub1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Unlike previous examples, it is unclear how we should make recursive calls in the last case, the one satisfying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(and (&amp;gt; n 0) (cons? l))&lt;/code&gt;. There are three possibilities:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(list-pick (rest l) (sub1 n))&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(list-pick l (sub1 n))&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(list-pick (rest l) n)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because this cannot be determined, we move on to the next step of the design recipe which is to fill in the implementation. By using the table technique from part II, the programmer should be able to determine quickly that the answer is simply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(list-pick (rest l) (sub1 n))&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This recipe can sometimes yield code that is longer than necessary. We can systematically simplify the implementation afterward.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;part-v-generative-recursion&quot;&gt;Part V: Generative Recursion&lt;/h3&gt;

&lt;p&gt;The first chapter of part V explores problems that cannot be solved with just structural recursion. Consider the design of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bundle&lt;/code&gt;, which chunks a list of length-1 strings (i.e. characters) into a list of length-&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; strings.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; [List-of 1String] N -&amp;gt; [List-of String]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; bundles chunks of s into strings of length n (or less)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bundle&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;;; Note that (implode &quot;abcdefg&quot;) outputs&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;;; (list &quot;a&quot; &quot;b&quot; &quot;c&quot; &quot;d&quot; &quot;e&quot; &quot;f&quot; &quot;g&quot;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;check-expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bundle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;implode&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;abcdefg&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
              &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;abc&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;def&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;g&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we walk through all three cases presented in the previous chapter carefully, we will see that none of the approaches can work.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;We definitely cannot consider any one of the arguments atomic because, from the example test case, we have to take apart both arguments.&lt;/li&gt;
  &lt;li&gt;We cannot recurse on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; by assuming they have the same size and proceeding in lockstep. This is close, but &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bundle&lt;/code&gt; clearly has to reset &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; periodically.&lt;/li&gt;
  &lt;li&gt;Treating both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; as independent decouples the arguments too much.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This problem requires generative recursion, which is more ad-hoc than structural recursion. It is called &lt;em&gt;generative&lt;/em&gt; recursion because it &lt;em&gt;generates&lt;/em&gt; sub-problems using a non-structural mean.&lt;/p&gt;

&lt;p&gt;For this kind of recursion, the design recipe is modified as follows:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;When writing the purpose statement of the function, we should also write how we plan to compute the result.&lt;/li&gt;
  &lt;li&gt;The corresponding template will be based on whether the given arguments are trivially solvable. All algorithms have roughly this organization:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generative-recursive-fun&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;problem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;trivially-solvable?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;problem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;determine-solution&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;problem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;combine-solutions&lt;/span&gt;
       &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;problem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generative-recursive-fun&lt;/span&gt;
         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generate-problem-1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;problem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
       &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generative-recursive-fun&lt;/span&gt;
         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generate-problem-n&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;problem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;We need to consider the following questions to fill in the implementation. Here are the questions, copied verbatim from the book.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;What is a trivially solvable problem?&lt;/li&gt;
    &lt;li&gt;How are trivial problems solved?&lt;/li&gt;
    &lt;li&gt;How does the algorithm generate new problems that are more easily solvable than the original one? Is there one new problem that we generate or are there several?&lt;/li&gt;
    &lt;li&gt;Is the solution of the given problem the same as the solution of (one of) the new problems? Or, do we need to combine the solutions to create a solution for the original problem? And, if so, do we need anything from the original problem data?&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;Finally, we should write a termination argument, explaining why our function eventually terminates. If the function does not terminate for all inputs, we should restrict the acceptable inputs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the example problem, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bundle&lt;/code&gt;, from earlier, the solution looks like the code below. Observe:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;the idea and the termination argument in the comment above &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bundle&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;the trivial case is when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(empty? s)&lt;/code&gt; is true&lt;/li&gt;
  &lt;li&gt;the non-trivial case generates the subproblem &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(bundle (drop s n) n)&lt;/code&gt; (Note the lack of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(first s)&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(rest s)&lt;/code&gt; in this solution.)&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; [List-of 1String] N -&amp;gt; [List-of String]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; bundles chunks of s into strings of length n&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; idea: take n items and drop n at a time&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; termination: (bundle s 0) loops unless s is &apos;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bundle&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cons&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;implode&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;take&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bundle&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;drop&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]))&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; [List-of X] N -&amp;gt; [List-of X]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; keeps the first n items from l if possible or everything&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;take&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;; [List-of X] N -&amp;gt; [List-of X]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; removes the first n items from l if possible or everything&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;drop&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Other classical examples include quicksort, binary search, and all kinds of mathematical examples like Euclidean’s algorithm, and Newton’s method.&lt;/p&gt;

&lt;p&gt;Since SICP uses these examples from the very start (when introducing recursion), it is harder for beginners to extract general principles that they can use to inform their coding process. By focusing on structural recursion first, HTDP gets the easy stuff out of the way and makes it explicit when students need to do some creative problem-solving.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;part-vi-accumulators&quot;&gt;Part VI: Accumulators&lt;/h3&gt;

&lt;p&gt;Structural recursion has the issue of the loss of knowledge: when a function recurses on a smaller list, it does not know whether this is the complete input or actually just a piece of another list. It loses knowledge about the context of this computation. This loss of knowledge could result in unnecessary, redundant computations. Even worse, it may be impossible to develop a working function at all.&lt;/p&gt;

&lt;p&gt;This part of the book introduces the idea of an accumulator variable, which helps mitigates this loss of knowledge. An accumulator variable is a variable that is passed alongside the usual input arguments. It satisfies an invariant, useful to this specific problem.&lt;/p&gt;

&lt;p&gt;Consider this example problem: We want to write a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;relative-&amp;gt;absolute&lt;/code&gt; that converts a list of relative to absolute distances by listing out the prefix sums. One solution is the following:&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; [List-of Number] -&amp;gt; [List-of Number]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; converts a list of relative to absolute distances&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; the first number represents the distance to the origin&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;relative-&amp;gt;absolute&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;rest-of-l&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;relative-&amp;gt;absolute&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
                  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;adjusted&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;add-to-each&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;rest-of-l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cons&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;adjusted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;check-expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;relative-&amp;gt;absolute&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;70&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
              &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;90&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;160&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;190&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;220&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
 
&lt;span class=&quot;c1&quot;&gt;; Number [List-of Number] -&amp;gt; [List-of Number]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; adds n to each number on l&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;add-to-each&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;check-expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cons&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;add-to-each&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;40&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;110&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;140&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;170&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
              &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;90&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;160&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;190&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;220&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This solution, however, has a running time of $O(n^2)$.&lt;/p&gt;

&lt;p&gt;To solve problems using accumulators, we need to figure out what the accumulator should represent, then we use the following template.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; Domain -&amp;gt; Range &lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;; Domain AccuDomain -&amp;gt; Range&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;; accumulator invariant ...&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;function/a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; expression involving a and sub-input of d&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;function/a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;d0&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;a0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that instead of designing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;function&lt;/code&gt; directly, we use the design recipe to design the accumulator version of the function, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;function/a&lt;/code&gt;. We would do structural recursion or generative recursion on the input arguments as usual, but the function should take extra arguments representing the accumulators. We can use the accumulators in the computation of the results if we assume and maintain appropriate invariants.&lt;/p&gt;

&lt;p&gt;For the example from above, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;relative-&amp;gt;absolute&lt;/code&gt;, we use the accumulator &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;accu-dist&lt;/code&gt; to keep track of the sum of the elements seen up until this point. We implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;relative-&amp;gt;absolute/a&lt;/code&gt; by performing structural recursion on the list &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt; but also taking in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;accu-dist&lt;/code&gt; as an extra argument&lt;sup id=&quot;fnref:proof&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:proof&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; [List-of Number] -&amp;gt; [List-of Number]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; converts a list of relative to absolute distances&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; the first number represents the distance to the origin&lt;/span&gt;
 
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;check-expect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;relative-&amp;gt;absolute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;v2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;70&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
              &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;50&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;90&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;160&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;190&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;220&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
 
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;relative-&amp;gt;absolute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;v2&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;; [List-of Number] Number -&amp;gt; [List-of Number]&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;; accumulator: accu-dist is the sum of distances seen so far&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;relative-&amp;gt;absolute/a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;accu-dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;accu&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;accu-dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cons&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;accu&lt;/span&gt;
                 &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;relative-&amp;gt;absolute/a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;accu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))])))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;relative-&amp;gt;absolute/a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l0&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The book suggests the following steps when solving any given problem:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Try to solve the problem with structural recursion. “If a structurally recursive function traverses the result of its natural recursion with an auxiliary, recursive function, consider the use of an accumulator parameter.” This is to optimize the solution’s time complexity.&lt;/li&gt;
  &lt;li&gt;If the problem has to be solved with generative recursion, an accumulator may be &lt;em&gt;required&lt;/em&gt; to obtain a correct, terminating algorithm. (For example, traversing a graph requires that we keep track of the visited notes, so we do not get stuck in a cycle.)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Taken to its extreme, this style becomes analogous to “iteration” (tail recursion) as taught in SICP.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:pure&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Pure functions do not have any side effects and always return the same output given the same input. &lt;a href=&quot;#fnref:pure&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:noloop&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Scheme (as taught) does not have looping constructs like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;while&lt;/code&gt;. Iteration has to be done through tail recursion. &lt;a href=&quot;#fnref:noloop&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:subset&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Or rather, a modified subset of Racket called BSL (Beginner Student Language), made for this book. &lt;a href=&quot;#fnref:subset&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:proof&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;To state the invariant more precisely: For all lists &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt; and values &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;accu-dist&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(add-to-each accu-dist l)&lt;/code&gt; gives the same result as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(relative-&amp;gt;absolute/a l accu-dist)&lt;/code&gt;. A formal proof of this function in Coq can be found &lt;a href=&quot;https://gist.github.com/tchomphoochan/0f8cda3cce088c136bdc050443781c4b&quot;&gt;here on Github&lt;/a&gt;. (I had too much fun writing this.) &lt;a href=&quot;#fnref:proof&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Wed, 05 Apr 2023 21:30:00 -0400</pubDate>
        <link>https://tcpc.me//2023/04/05/htdp-review.html</link>
        <guid isPermaLink="true">https://tcpc.me//2023/04/05/htdp-review.html</guid>
        
        <category>Programming</category>
        
        <category>Review</category>
        
        <category>Summary</category>
        
        
      </item>
    
      <item>
        <title>How to derive the Y combinator</title>
        <description>&lt;p&gt;I forgot my favorite method of deriving Y combinator for the n-th time, so this time I am forcing myself to write this up as a quick reminder.&lt;/p&gt;

&lt;p&gt;For a proper tutorial, consider this:
&lt;a href=&quot;https://homes.cs.washington.edu/~sorawee/en/blog/2017/10-05-deriving-Y.html&quot;&gt;https://homes.cs.washington.edu/~sorawee/en/blog/2017/10-05-deriving-Y.html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For each example here, verify with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fact(10) == 3628800&lt;/code&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;version-0&quot;&gt;Version 0&lt;/h2&gt;

&lt;p&gt;This definition of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fact&lt;/code&gt; requires that its body can look up the name &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fact&lt;/code&gt; in its parent environment, which is bad.&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We want to be able to write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fact&lt;/code&gt; entirely in terms of anonymous functions which do not have free variables. These things have a fancy name: “combinators.”&lt;/p&gt;

&lt;p&gt;Y combinator, which we will eventually reinvent, is of interest because it provides us with a primitive for building recursive functions in languages that provide higher-order functions but not recursion by default.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;version-1&quot;&gt;Version 1&lt;/h2&gt;

&lt;p&gt;The trick is to pass in the recursion as an argument instead.&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# this relies on currying/partial application
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;version-2&quot;&gt;Version 2&lt;/h2&gt;

&lt;p&gt;Because there are no circular dependencies, we can just inline everything. We can just stop here if we want.&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;version-3&quot;&gt;Version 3&lt;/h2&gt;

&lt;p&gt;I hate code duplication, so I am writing a helper function to call the function on itself instead.&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Suppose I define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make_recur = lambda f: f(f)&lt;/code&gt;, then the user could make their own recursive function just by writing a function that takes in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt;. To make a recursive call, write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f(f)(...)&lt;/code&gt;. Here is an example of how to use it.&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;make_recur&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;make_recur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# fib(10) == 55
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;version-4-broken&quot;&gt;Version 4 (broken)&lt;/h2&gt;

&lt;p&gt;This is where I usually get stuck.&lt;/p&gt;

&lt;p&gt;Right now, requiring the user to write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f(f)(...)&lt;/code&gt; is disgusting. We want to allow the user to write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f(...)&lt;/code&gt; instead.&lt;/p&gt;

&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;???&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Our task is to figure out the definition of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fact_base&lt;/code&gt; to wrap around &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_fact_base&lt;/code&gt;. Recall, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fact_base&lt;/code&gt; takes in a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; as its argument. It should call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_fact_base&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f(f)&lt;/code&gt; already prepped in that argument slot. This should work:&lt;/p&gt;

&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Actually, this results in stack overflow because Python uses strict evaluation.&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
     &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
     &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
     &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# infinite recursion
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;version-5&quot;&gt;Version 5&lt;/h2&gt;

&lt;p&gt;Realize that the issue is that Python evaluates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f(f)&lt;/code&gt; before passing into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_fact_base&lt;/code&gt;. We do not want this to be evaluated unless it is necessary (i.e. when function body requires a recursive call). The trick is to simply wrap the expression &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f(f)&lt;/code&gt; in a somewhat redundant lambda expression (make the argument explicit).&lt;/p&gt;

&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we can inline everything if we want.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;the-y-combinator&quot;&gt;The Y combinator&lt;/h2&gt;

&lt;p&gt;What we discovered in version 4 was the Y combinator. Specifically:&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Y = lambda f: (lambda x: x(x))(lambda x: f(x(x)))
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;$Y = \lambda f. (\lambda x. f (x x))(\lambda x. f (x x))$.&lt;/p&gt;

&lt;p&gt;Some people might like to write: $Y = \lambda f. (\lambda x. x x)(\lambda x. f (x x))$.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;the-z-combinator&quot;&gt;The Z combinator&lt;/h2&gt;

&lt;p&gt;What we discovered in version 5 was the Z combinator. Specifically:&lt;/p&gt;

&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Z = lambda f: (lambda x: x(x))(lambda x: f(lambda v: x(x)(v)))
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_fact_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;$Y = \lambda f. (\lambda x. f (\lambda v. x x v))(\lambda x. f (\lambda v. x x v))$.&lt;/p&gt;

&lt;p&gt;Again, you can condense this into $Y = \lambda f. (\lambda x. x x)(\lambda x. f (\lambda v. x x v))$.&lt;/p&gt;

&lt;p&gt;This works in Python.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;another-way-to-derive-the-y-combinator&quot;&gt;Another way to derive the Y combinator&lt;/h2&gt;

&lt;p&gt;Here is another way I like to use to memorize how to write the Y combinator. This is probably not very understandable if you have not seen it before.&lt;/p&gt;

&lt;p&gt;First, you need to know that $Y$ finds fixed point of a function, namely $Y f$ gives the input $x$ such that $f x = x$, i.e. $f (Y f) = Y f$.&lt;/p&gt;

&lt;p&gt;We can repeatedly substitute $Y f$ with $f (Y f)$. So, $Y f = f (Y f) = f (f (Y f)) = f (f (f (f \dots)))$.&lt;/p&gt;

&lt;p&gt;This equation, $Y f = f (f (f (f \dots)))$, is the goal we are trying to achieve. How do we apply $f$ repeatedly?&lt;/p&gt;

&lt;p&gt;Well, let’s answer a simpler question first: How do you make something run infinitely?&lt;/p&gt;

&lt;p&gt;This works: $(\lambda x. x x)(\lambda x. x x)$.&lt;/p&gt;

&lt;p&gt;Notice that if you keep simplifying the expression, you are always stuck at $(\lambda x. x x)(\lambda x. x x)$.&lt;/p&gt;

&lt;p&gt;We just need to sneak $f$ in there: $(\lambda x. f (x x))(\lambda x. f (x x))$.&lt;/p&gt;

&lt;p&gt;Observe $(\lambda x. f (x x))(\lambda x. f (x x)) = f (\lambda x. f (x x))(\lambda x. f (x x)) = f (f (\lambda x. f (x x))(\lambda x. f (x x))) = f (f (f \dots))$.&lt;/p&gt;

&lt;p&gt;We are done! $Y = \lambda f. (\lambda x. f (x x))(\lambda x. f (x x))$.&lt;/p&gt;

&lt;p&gt;To get the Z combinator, just add a redundant $v$:
$Z = \lambda f. (\lambda x. f (\lambda v. x x v))(\lambda x. f (\lambda v. x x v)) $.&lt;/p&gt;

&lt;p&gt;Yay!&lt;/p&gt;
</description>
        <pubDate>Thu, 30 Dec 2021 01:20:00 -0500</pubDate>
        <link>https://tcpc.me//2021/12/30/y-combinator.html</link>
        <guid isPermaLink="true">https://tcpc.me//2021/12/30/y-combinator.html</guid>
        
        <category>Programming</category>
        
        <category>Notes</category>
        
        
      </item>
    
      <item>
        <title>Power tripping with infinite lists in Haskell</title>
        <description>&lt;p&gt;Lately I have been forcing many of my friends to listen/watch this mini-lecture of mine about Haskell and infinite streams. I thought it was pretty cool, so I wanted to share it here too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;: about a year of programming experience? (College is fine.) Familiarity with basic algorithms and data structures (specifically linked list) and programming paradigms/concepts (OOP, generators, higher order functions, etc.) to appreciate this. Calculus, up to power series.&lt;/p&gt;

&lt;p&gt;I will start with explaining how programming in Haskell works roughly, then dive into some toy examples using Haskell’s lazy evaluation feature, and then end with something that hopefully blows your mind. (Please be kind to me if it doesn’t.) There are small exercises along the way you should do (or check the solutions) &lt;strong&gt;before&lt;/strong&gt; proceeding to the next session.&lt;/p&gt;

&lt;p&gt;If you want to try running code, &lt;a href=&quot;https://www.haskell.org/downloads/&quot;&gt;download Haskell&lt;/a&gt;. As far as I know there aren’t really any good Haskell interactive interpreter online. &lt;a href=&quot;https://tryhaskell.org/&quot;&gt;tryhaskell.org&lt;/a&gt; only lets you try simple expressions. You can’t do definitions.&lt;/p&gt;

&lt;p&gt;Ready, set, go!&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/lazy-power-series-integration/haskellanime.png&quot; alt=&quot;&quot; width=&quot;80%&quot; /&gt;
Because anime girls holding programming books is always the best cover for a blog post.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;haskell&quot;&gt;Haskell&lt;/h2&gt;

&lt;p&gt;Haskell is a functional programming language. What that means is a lot of what you write in Haskell are based on functions and how they compose. Specifically, you write pure functions: no mutating states like setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x = 5&lt;/code&gt; then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x = 10&lt;/code&gt; after, and no producing side effects like modifying an array outside the function.&lt;/p&gt;

&lt;p&gt;This style of programming seems unnecessarily restrictive to the unfamiliar. However, its constraints open up a whole new level of expressivity (at least for some kind of problems). This is most easily seen at first glance from Haskell’s syntax. See this Fibonacci function definition.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Haskell’s function definitions feel almost like mathematics. You are not defining &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib&lt;/code&gt; to be a function that computes this and that and returns the combination. You are defining what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib&lt;/code&gt; evaluated at some value should &lt;em&gt;be&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Here’s another example. This defines what it means for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; to divide &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt;, for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; to be a prime number, and what the list of prime numbers should contain. Pretty cool. (We’re not focusing too much on the syntax itself here.)&lt;/p&gt;
&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;divides&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;                     &lt;span class=&quot;c1&quot;&gt;-- a divides b if remainder of b divided by a is zero&lt;/span&gt;
                                                   &lt;span class=&quot;c1&quot;&gt;-- note that haskell allows infix notation for binary operators, using backticks (`)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;isPrime&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;divides&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;-- n is prime number if none of 2 to n-1 divides n&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;primes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isPrime&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;                      &lt;span class=&quot;c1&quot;&gt;-- primes is a list of numbers from 2 onward that satisfies isPrime&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;primes&lt;/code&gt; is an infinite list. Haskell can run these definitions just fine because the list is (to some degree of oversimplification) lazily evaluated. Only when you try to, say, request the fifth element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;primes&lt;/code&gt;, it bothers looking at numbers starting from 2 to see which ones are primes. As soon as it finds what the fifth prime is, it stops.
If you write something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;take 10 primes&lt;/code&gt; in the interactive console, you will see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[2,3,5,7,11,13,17,19,23,29]&lt;/code&gt; (It takes the first ten elements.). If you just write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;primes&lt;/code&gt;, then it will keep printing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[2,3,5,7,...&lt;/code&gt; indefinitely, because Haskell does not really know that the list is infinite. It just keeps evaluating and printing until it gets to the end (It won’t.).&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;linked-lists&quot;&gt;Linked lists&lt;/h2&gt;

&lt;p&gt;In Haskell, lists are better thought of as linked lists rather than dynamically sized arrays. Namely:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;There is an empty list &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[]&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Other lists consist of an element as its head and another linked list as its tail, representing the rest of the list. This is denoted using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt; operator. (In Scheme, this is called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons&lt;/code&gt;.)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, the list &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1,2,3,4,5]&lt;/code&gt; is equivalent to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1:[2,3,4,5]&lt;/code&gt;, which is equivalent to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1:2:3:4:5:[]&lt;/code&gt;. Note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1,2]:[3,4,5]&lt;/code&gt; does not really make sense here. That would mean &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1,2]&lt;/code&gt; is the first element and the rest of the elements are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5&lt;/code&gt;. So, the list would be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[1,2],3,4,5]&lt;/code&gt; which is not allowed in Haskell because all elements of the list should be of the same type.&lt;/p&gt;

&lt;p&gt;When viewed this way, lazy evaluation is a bit easier to grasp. If you only ever need the first element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x:xs&lt;/code&gt;, you only have to evaluate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;. There is no point in evaluating &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xs&lt;/code&gt;. So, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1,2..]&lt;/code&gt; which is basically just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1:&amp;lt;something I don&apos;t care until I need it&amp;gt;&lt;/code&gt; can be defined just fine. (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xs&lt;/code&gt; are conventional names used to denote head and tail of a list.)&lt;/p&gt;

&lt;p&gt;Now, let’s try to define some simple lists by ourselves. The first one is list of positive integers (equivalent to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1,2..]&lt;/code&gt;).&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;integersFrom&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;integersFrom&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;integers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;integersFrom&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This says: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integersFrom n&lt;/code&gt; is a list consisting of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; as its first element and the rest is the list &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integersFrom (n+1)&lt;/code&gt;, i.e. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integersFrom n&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[n,n+1,n+2..]&lt;/code&gt;. Then, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integers&lt;/code&gt; is just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integersFrom 1&lt;/code&gt;. Nice.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;https://i.imgflip.com/2kaaae.jpg&quot; alt=&quot;&quot; width=&quot;80%&quot; /&gt;
People who aren’t functional programmers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;recursively-defined-lists&quot;&gt;Recursively defined lists&lt;/h2&gt;

&lt;p&gt;To be honest, that one was kinda boring. Let’s try another approach. First, I’m going to define an infinite list of repeating ones.&lt;/p&gt;
&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Woah—&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ones&lt;/code&gt; is defined in terms of itself and it is not even a function! This is usually not allowed in other languages. However, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ones&lt;/code&gt; can be thought of as a function with zero arguments. Its body is lazily evaluated wherever possible. So, if you are just accessing the first element (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;head ones&lt;/code&gt;) which is just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;, you are fine. If you are trying to access the second element—Well, the second element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ones&lt;/code&gt; is the first element of the rest of the list, which is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ones&lt;/code&gt;, and we know the first element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ones&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;, so the second element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ones&lt;/code&gt; overall is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Now, we can redefine &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integers&lt;/code&gt; without needing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integersFrom n&lt;/code&gt; at all.&lt;/p&gt;
&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;integers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zipWith&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;integers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This says: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integers&lt;/code&gt; is a list that starts with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;, and the rest of the elements are the lists &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ones&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integers&lt;/code&gt; added coordinate-wise (That’s what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zipWith (+)&lt;/code&gt; means). **Verify for yourself that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integers&lt;/code&gt; is indeed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1,2...].** Again, see that we are defining &lt;/code&gt;integers` in terms of itself without taking in any arguments at all. We are not modifying any external variables either.&lt;/p&gt;

&lt;p&gt;Here’s another exercise: &lt;strong&gt;Define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;powersOfTwo&lt;/code&gt;.&lt;/strong&gt; You should get &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1,2,4,8,16,32,64,128]&lt;/code&gt; when you execute &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;take 8 powersOfTwo&lt;/code&gt; (take first eight elements). See footnote for solution&lt;sup id=&quot;fnref:powers&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:powers&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; (Please try not to).&lt;/p&gt;

&lt;p&gt;Another one to try: &lt;strong&gt;Define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fibs&lt;/code&gt; recursively (without using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib&lt;/code&gt; from earlier).&lt;/strong&gt; The output of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;take 9 fibs&lt;/code&gt; should look like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[0,1,1,2,3,5,8,13,21]&lt;/code&gt;. As a hint, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tail li&lt;/code&gt; gives you the tail of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;li&lt;/code&gt; (everything but the head). You will probably also need two head elements (base case) rather than just one. See footnote for solution&lt;sup id=&quot;fnref:fib&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:fib&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; (Please do if you’re stuck).&lt;/p&gt;

&lt;p&gt;As a fun side note, it is also possible to define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;primes&lt;/code&gt; recursively (instead of checking for division by all positive integers except &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;). Though, this is more complicated than it seems at first glance because your definition can be end up being “so lazy that it ends up biting its own tail” (see: https://stackoverflow.com/a/25819438). I’m leaving the code here for people who are curious.&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;divides&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;isPrime&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;divides&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;takeWhile&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;floor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fromIntegral&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;primes&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;-- filter doesn&apos;t know when to stop, so need to use takeWhile instead&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;primes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isPrime&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;-- primes have to be primed (ha) with 2 as its first element or takeWhile doesn&apos;t know when to stop&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;infinite-series-and-integration&quot;&gt;Infinite series and integration&lt;/h2&gt;

&lt;p&gt;This is where things get fun. Let’s consider an arbitrary infinite power series $p(x) = a_0 + a_1 x + a_2 x^2 + a_3 x^3 + \cdots $ where $a_0, a_1, a_2, \cdots$ are constants. We will try to represent this series with an infinite list of integers &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[a0, a1, a2, ...]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As a concrete example, the exponential series $e^x = \sum_{n=1}^{\infty} \frac{x^n}{n!} = 1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \frac{x^4}{24}$ would be represented as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1, 1, 1/2, 1/6, 1/24, ...]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Adding two power series up is easy: just add coordinate-wise. Multiplying two power series is a bit more difficult. (Try!) We’ll focus on integration instead. Note that when you integrate $p(x)$ you get&lt;/p&gt;

&lt;p&gt;$\int p(x) \, dx = c + \frac{a_0}{1}x + \frac{a_1}{2}x^2 + \frac{a_2}{3}x^3 + \frac{a_3}{4}x^4 + \cdots$&lt;/p&gt;

&lt;p&gt;where $c$ is the constant of integration. This looks like dividing the list &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[a0, a1, a2, ...]&lt;/code&gt; by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1, 2, 3, ...]&lt;/code&gt; and prepending the constant &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt; to the front so the exponentials are increased accordingly. So, I will define integration like this.&lt;/p&gt;
&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;integrate&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zipWith&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;integers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Note that the constant has &lt;em&gt;not&lt;/em&gt; been prepended yet, so the correct way to integrate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt; would be to write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c : integrate p&lt;/code&gt; where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt; is the constant.&lt;/p&gt;

&lt;p&gt;As you might recall, the derivative of $e^x$ is $e^x$ itself. In other word, $e^x$ is $e^x$ integrated, with the constant of integration set so that $e^0 = 1$. Namely, $e^x = 1 + \int_0^{x} e^{x’} \, dx’$.&lt;/p&gt;

&lt;p&gt;We can write this in Haskell.&lt;/p&gt;
&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;expSeries&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;integrate&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expSeries&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;take&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expSeries&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- outputs: [1.0,1.0,0.5,0.16666666666666666,4.1666666666666664e-2]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- matches [1, 1, 1/2, 1/6, 1/24]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Let that sink in for a moment.&lt;/strong&gt; We defined &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;expSeries&lt;/code&gt; in terms of the integral of &lt;em&gt;itself&lt;/em&gt;. We did not give Haskell any other information at all (aside from how integration works, in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integrate&lt;/code&gt;). Yet, Haskell handles this definition with grace and gives us the correct answer when we try to inspect.&lt;/p&gt;

&lt;p&gt;If you try to evaluate the first 100 terms of the series for $x=10$, the answer is pretty close:&lt;/p&gt;
&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;series&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;evalAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foldr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;take&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nTerms&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nTerms&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;-- This just means &quot;evaluate the first n terms.&quot; Won&apos;t explain how this works today.&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;expSeries&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;evalAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- outputs: 22026.465794806714&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- compare that to:&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- outputs: 22026.465794806718&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Damn.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;https://i.redd.it/9vfjzu7pbt801.png&quot; alt=&quot;&quot; width=&quot;80%&quot; /&gt;
I’ve been trying to find a meme about infinite series online but I couldn’t find a good one.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;the-final-blow&quot;&gt;The final blow&lt;/h2&gt;

&lt;p&gt;This is around the point where I desperately look at you and ask for your validation: Is your mind blown yet? If not, please pretend it is anyway so I don’t get sad. 🥺&lt;/p&gt;

&lt;p&gt;We’ll finish with one last example. Remember how the derivative of $\sin x$ is $\cos x$ and the derivative of $\cos x$ is $-\sin x$?. About that…&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;sineSeries&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;integrate&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cosineSeries&lt;/span&gt;               &lt;span class=&quot;c1&quot;&gt;-- sin 0 == 0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cosineSeries&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;negate&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integrate&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sineSeries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;-- cos 0 == 1&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;take&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sineSeries&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- outputs: [0.0,1.0,-0.0,-0.16666666666666666,0.0,8.333333333333333e-3,-0.0,-1.984126984126984e-4,0.0,2.7557319223985893e-6]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;take&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cosineSeries&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- outputs: [1.0,-0.0,-0.5,0.0,4.1666666666666664e-2,-0.0,-1.388888888888889e-3,0.0,2.48015873015873e-5,-0.0]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Check.&lt;/p&gt;

&lt;p&gt;$\sin x =  x - \frac{x^3}{3} + \frac{x^5}{5} + \cdots$&lt;/p&gt;

&lt;p&gt;$\cos x = 1 - \frac{x^2}{2} + \frac{x^4}{4} + \cdots$&lt;/p&gt;

&lt;p&gt;Have a good day.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;https://i.kym-cdn.com/photos/images/original/002/128/164/13b.png&quot; alt=&quot;&quot; width=&quot;70%&quot; /&gt;
Don’t you wish Haskell uses actual fractions by default like Scheme does?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:powers&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;powersOfTwo = 1 : zipWith (+) powersOfTwo powersOfTwo&lt;/code&gt;. &lt;a href=&quot;#fnref:powers&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:fib&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fibs = 0 : 1 : (zipWith (+) fibs (tail fibs))&lt;/code&gt; &lt;a href=&quot;#fnref:fib&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Mon, 20 Dec 2021 17:37:00 -0500</pubDate>
        <link>https://tcpc.me//2021/12/20/lazy-power-series-integration.html</link>
        <guid isPermaLink="true">https://tcpc.me//2021/12/20/lazy-power-series-integration.html</guid>
        
        <category>Programming</category>
        
        
      </item>
    
      <item>
        <title>Reading log: SICP (the wizard book) is cool</title>
        <description>&lt;p&gt;&lt;em&gt;In which I nerd about SICP.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mitpress.mit.edu/sites/default/files/sicp/index.html&quot;&gt;Structure and Interpretation of Computer Programs&lt;/a&gt; (SICP) is a very old, introductory programming book. It was written in 1985 and was subsequently edited in 1996.&lt;/p&gt;

&lt;p&gt;It is based on a language called Scheme, a Lisp dialect which at first seems like a strange and impractical language.&lt;/p&gt;

&lt;p&gt;Yet, apparently, many people consider SICP to be one of the best books ever written about computer programming.
Some people preach endlessly about how understanding Lisp is akin to attaining enlightenment, something that can change the way you think about programming forever.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/sicp-is-cool/gabriel-wizard-book.jpg&quot; alt=&quot;&quot; width=&quot;80%&quot; /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I got curious, because last spring I wrote a Scheme interpreter for my Python class&lt;sup id=&quot;fnref:py&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:py&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; homework, and while that gave me a good overview of the language, I couldn’t see how Lisps are special.&lt;/p&gt;

&lt;p&gt;So, I decided to go down the rabbit hole and read SICP.
(This is partly because I have nothing better to do this summer.)&lt;/p&gt;

&lt;p&gt;The result: I did not get that alleged “enlightenment” and 100x productivity boost but oh boy were there a lot of fascinating concepts and elegant explanations.&lt;/p&gt;

&lt;p&gt;I’m writing this blog post to catalog some of the ideas I thought were really neat. Perhaps you might find them interesting too!&lt;/p&gt;

&lt;blockquote class=&quot;infobox no_toc&quot;&gt;
  &lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;
&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#syntactic-uniformity-and-metaprogramming&quot; id=&quot;markdown-toc-syntactic-uniformity-and-metaprogramming&quot;&gt;Syntactic uniformity and metaprogramming&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#procedures-enable-storing-data&quot; id=&quot;markdown-toc-procedures-enable-storing-data&quot;&gt;Procedures enable storing data&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rediscovering-church-numerals&quot; id=&quot;markdown-toc-rediscovering-church-numerals&quot;&gt;Rediscovering church numerals&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#infinite-streams-are-trippy&quot; id=&quot;markdown-toc-infinite-streams-are-trippy&quot;&gt;Infinite streams are trippy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;syntactic-uniformity-and-metaprogramming&quot;&gt;Syntactic uniformity and metaprogramming&lt;/h2&gt;

&lt;p&gt;Scheme’s syntax is very simple.&lt;/p&gt;

&lt;p&gt;Everything is an expression. Expressions (almost) always evaluate to a value.&lt;/p&gt;

&lt;p&gt;There are numbers and strings, like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;hi&quot;&lt;/code&gt;. These evaluate to themselves.&lt;/p&gt;

&lt;p&gt;There are symbols, like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sqrt&lt;/code&gt;. These evaluate to some stored values or function objects.&lt;/p&gt;

&lt;p&gt;Then, most importantly, there are S-expressions, which are basically a way to describe lists. They look like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(3 4 5 6)&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(1 (2 3) () (()))&lt;/code&gt;. Some S-expressions can be evaluated, and some can’t.&lt;/p&gt;

&lt;p&gt;When Scheme sees an S-expression, the following evaluation rule applies:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;First, evaluate the first item in the list. Hopefully you get a function object (otherwise Scheme complains).&lt;/li&gt;
  &lt;li&gt;Then, evaluate the rest of the list (recursively) and call the function object using those items as arguments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, you can do something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(+ 3 (* 5 5))&lt;/code&gt;, which will evaluate to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;28&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Note that what are usually considered “operations” in other languages are pretty much the same as functions here, like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;=&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are “special forms” which are exceptions to the evaluation rule described earlier.
If the first element signals a special form, then the rest of the list are evaluated (or not) based on that form’s special rules.
Special forms include:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(define &amp;lt;var&amp;gt; &amp;lt;expr&amp;gt;)&lt;/code&gt; which assigns the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;expr&amp;gt;&lt;/code&gt;, evaluated, to a variable named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;var&amp;gt;&lt;/code&gt;. (Similarly, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set!&lt;/code&gt; mutates a defined variable.)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(if &amp;lt;cond&amp;gt; &amp;lt;true-expr&amp;gt; &amp;lt;false-expr&amp;gt;)&lt;/code&gt; which first evaluates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;cond&amp;gt;&lt;/code&gt;; if it is true, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;true-expr&amp;gt;&lt;/code&gt; is evaluated and returned, otherwise &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;false-expr&amp;gt;&lt;/code&gt; is.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(lambda (&amp;lt;arg1&amp;gt; &amp;lt;arg2&amp;gt; &amp;lt;...&amp;gt;) &amp;lt;body&amp;gt;)&lt;/code&gt; which creates a function object that whenever called, takes some number of arguments and evaluates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;body&amp;gt;&lt;/code&gt;. The function is evaluated in an environment derived from the environment in which it was defined&lt;sup id=&quot;fnref:lexical&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:lexical&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example of how you would compute Fibonacci numbers.&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;fib&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
           &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; could evaluate to the anonymous function object or&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; evaluate to nothing, depending on the implementation.&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; evaluates to 0 (and is displayed on the screen)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; evaluates to 1&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; evaluates to 55&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; evaluates to 55&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;We create an anonymous function object taking a single argument is defined using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lambda&lt;/code&gt; special form, and then bind that to the name &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib&lt;/code&gt;&lt;sup id=&quot;fnref:functionshorthand&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:functionshorthand&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;. We call the function object with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(fib n)&lt;/code&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;This syntax, full of parentheses, might look offputting at first, but consider this code which reads from user input and mutates the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; accordingly:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;set!&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;good&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;set!&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;bad&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;You can actually write this equivalently:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;set!&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;good&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;bad&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Python, the first code would look like this:&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;good&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;bad&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The second code would look like this:&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;good&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;bad&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;They look similar, but they rely on completely different syntaxes.
The first one uses an if-else control construct which requires the indented code inside to be statements.
The second one uses a ternary operator which only works with expressions and is much more limited in capability.
(Other languages like C use different symbols for the ternary operator altogether. Some languages don’t even have the ternary operator.)&lt;/p&gt;

&lt;p&gt;Here’s another example involving arithmetic operators, in Scheme:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; (define (fn args) body) is a shorthand for (define fn (lambda (args) body))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;a-plus-abs-b&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;There is no way to write this in Python, because Python does not provide operators as functions (unless you import or build them yourself). The closest approximation is:&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;a_plus_abs_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sub&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In a way, Lisp syntax is less arbitrary than that of any other languages. Some people consider this a “more mathematically pure” representation of a program. (Indeed, Lisp is modeled off lambda calculus.)&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Now, let’s go back to S-expressions. If you hand the source code above to the evaluator, you would think of it as code which is to be executed. However, you can probably make a case that this is simply a list of three items, two of which are symbols and the final one another list:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fib&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Similarly, you can also claim that the following to-do list is not just data, but is also &lt;em&gt;code&lt;/em&gt;&lt;sup id=&quot;fnref:defmacro&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:defmacro&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;todo&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;housework&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;priority&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;high&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Clean the house.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;priority&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;medium&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Wash the dishes.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;priority&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;medium&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Buy more soap.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Why? Well, you can feed this into a custom evaluator built specifically for parsing to-do lists stored as S-expressions. (There are a lot of XML parsers around. S-expressions aren’t that different.)&lt;/p&gt;

&lt;p&gt;Or perhaps, even better, feed this into a Scheme interpreter that has special forms &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;todo&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;item&lt;/code&gt; built in, and these special forms happen to transform this data into Scheme S-expressions that can actually be evaluated.&lt;/p&gt;

&lt;p&gt;This is where Scheme’s uniform syntax shines.&lt;/p&gt;

&lt;p&gt;Like any other languages, Scheme provides functions that let you manipulate lists (and by extension, trees, because trees can be represented as nested lists). If Scheme’s code is naturally a list, then that means you can manipulate the code pretty much directly.&lt;/p&gt;

&lt;p&gt;Scheme also provides facilities for you to define your own special forms by specifying how the arguments are re-arranged. (These are called macros.)
For example, you can define a special form &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(loop &amp;lt;var1&amp;gt; &amp;lt;var2&amp;gt; &amp;lt;...&amp;gt; from &amp;lt;begin-val&amp;gt; up to &amp;lt;end-val&amp;gt; &amp;lt;body&amp;gt;)&lt;/code&gt; that lets you do nested loops with all variables ranging from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;begin-val&amp;gt;&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;end-val&amp;gt;&lt;/code&gt;. Wherever the compiler sees a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;loop&lt;/code&gt; form with one variable, it knows to substitute with this code for you&lt;sup id=&quot;fnref:hygiene&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:hygiene&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; generate a recursive function to loop up from &amp;lt;begin&amp;gt; to &amp;lt;end&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generated-loop-1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;var1&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; body will have access to the variable &amp;lt;var&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;var&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;end-val&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generated-loop-1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;var&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; then call it&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generated-loop-1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;begin-val&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Or with this code if there are two variables:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generated-loop-1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;var1&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generated-loop-2&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;var2&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;var2&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;end-val&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generated-loop-2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;var2&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generated-loop-2&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;begin-val&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;var1&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;end-val&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generated-loop-1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;var1&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;generated-loop-1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;&amp;lt;begin-val&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This cannot be done with functions&lt;sup id=&quot;fnref:lambdasub&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:lambdasub&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;6&lt;/a&gt;&lt;/sup&gt;. Otherwise, Scheme would attempt to evaluate at runtime the symbols &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;from&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;up&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to&lt;/code&gt; in the arguments, which are meaningless, and even worse, the body will be evaluated with variables that aren’t even defined yet.&lt;/p&gt;

&lt;p&gt;Macros exist in languages like C, but they work via text substitutions only.
Macros can be controlled through special keywords like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#define&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#ifdef&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#ifndef&lt;/code&gt;, but that’s about it.&lt;/p&gt;

&lt;p&gt;Meanwhile, in Scheme, we can use Scheme’s existing functionalities to manipulate the code however needed, like in the example earlier we might use recursion to recursively generate the code.&lt;/p&gt;

&lt;p&gt;If it was possible to write macros in C using C’s full functionalities, you would still have to manually write strings at character level, accounting for each original syntax you want to represent (like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; with curly braces, and semicolons at the end of each statement).
In Scheme, you just have to find/insert/delete/edit/duplicate list elements and not worry about the code’s textual representation.&lt;/p&gt;

&lt;p&gt;I guess this is what people mean when they talk about the duality between code and data&lt;sup id=&quot;fnref:extrapolate&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:extrapolate&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;7&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;procedures-enable-storing-data&quot;&gt;Procedures enable storing data&lt;/h2&gt;

&lt;p&gt;The entire second chapter of SICP is about building abstractions.&lt;/p&gt;

&lt;p&gt;The book emphasizes a sensible design of “abstraction barriers” or interfaces, which allow programmers to reason about the correctness of their code at each level separately and maintain modularity.&lt;/p&gt;

&lt;p&gt;Even though Scheme does not have built-in support for object oriented programming, SICP went on to show multiple ways one could invent OOP, together with the motivation for each approach&lt;sup id=&quot;fnref:badoop&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:badoop&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;8&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;While those concepts are important, what actually caught my attention was how powerful functions turn out to be.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Let’s take &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons&lt;/code&gt; for example. In Scheme, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons&lt;/code&gt; is the primary data structure for building any kind of compound data.&lt;/p&gt;

&lt;p&gt;The function call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cons a b)&lt;/code&gt; creates a cons object with elements &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt;. This is like a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pair&lt;/code&gt; in C++ or a tuple with two elements in Python.&lt;/p&gt;

&lt;p&gt;To access the first element, you use the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;car&lt;/code&gt; on the cons object.
Likewise, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cdr&lt;/code&gt; is for accessing the second element.&lt;sup id=&quot;fnref:carcdr&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:carcdr&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;9&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;By chaining cons together, you can build a linked list where the first element of the list is in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;car&lt;/code&gt; and the rest of the list is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cdr&lt;/code&gt;. An empty list is represented by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nil&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, evaluating &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cons (cons 1 nil) (cons 2 (cons 3 nil)))&lt;/code&gt; gives the list represented as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;((1) 2 3)&lt;/code&gt;. (The first element is a list with one element. The second and third elements are numbers.)
To get the third element, you write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(car (cdr (cdr x)))&lt;/code&gt; where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; is the list. This evaluates to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From here, you can implement plenty of abstract data structures, like sets and dictionaries, albeit very inefficient. You can also build trees. (In practice, you would use data structures provided as primitives, which are a lot more efficient.)&lt;/p&gt;

&lt;p&gt;At this point, we have considered &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons&lt;/code&gt; to be one of the language’s primitives and treat it like magic. We don’t really know how it’s implemented.&lt;/p&gt;

&lt;p&gt;For all we know, these are the only conditions we care about, “axioms” of some sort:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(car (cons a b))&lt;/code&gt; evaluates to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cdr (cons a b))&lt;/code&gt; evaluates to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, what if we redefine &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;car&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cdr&lt;/code&gt; like this?&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cons&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;car&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cdr&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cons a b)&lt;/code&gt; creates a function object which evaluates to the saved values &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; depending on the argument.
Then, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;car&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cdr&lt;/code&gt; simply call the function object with the correct argument.
So, for all intents and purposes, this definition is perfectly valid.&lt;/p&gt;

&lt;p&gt;The concept that procedures are like data, in the sense that they can be passed around, is already well-known.&lt;/p&gt;

&lt;p&gt;This example takes it a step further. It shows that procedures &lt;em&gt;enable&lt;/em&gt; storing of data.&lt;/p&gt;

&lt;p&gt;We don’t use any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;define&lt;/code&gt; to store values at all (except for the function names).
Yet, we can achieve the illusion of doing so by creating anonymous functions that are bound to specific contexts.&lt;sup id=&quot;fnref:acceptmodel&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:acceptmodel&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;10&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;rediscovering-church-numerals&quot;&gt;Rediscovering church numerals&lt;/h2&gt;

&lt;p&gt;The book took this concept of encapsulation further by asking us to do this execise.&lt;/p&gt;

&lt;blockquote class=&quot;infobox&quot;&gt;
  &lt;p&gt;&lt;strong&gt;Exercise 2.6.&lt;/strong&gt; In case representing pairs as procedures wasn’t mind-boggling enough, consider that, in a language that can manipulate procedures, we can get by without numbers (at least insofar as nonnegative integers are concerned) by implementing 0 and the operation of adding 1 as&lt;/p&gt;

  &lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;zero&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;add-1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;This representation is known as Church numerals, after its inventor, Alonzo Church, the logician who invented the $\lambda$ calculus.&lt;/p&gt;

  &lt;p&gt;Define one and two directly (not in terms of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zero&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add-1&lt;/code&gt;). (Hint: Use substitution to evaluate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(add-1 zero)&lt;/code&gt;). Give a direct definition of the addition procedure &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt; (not in terms of repeated application of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add-1&lt;/code&gt;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This was the first time I had to interpret complicated nested lambda expressions myself without knowing beforehand the expected behavior. (Conditionals were much easier to invent.)&lt;/p&gt;

&lt;p&gt;Slowly working through the substitutions until I saw the patterns did the trick.
It made me more comfortable with reading other lambda expressions, including understanding the Y-combinator I couldn’t quite grasp before&lt;sup id=&quot;fnref:lambdavideo&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:lambdavideo&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;11&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;As it turns out, a number $n$ is represented by a function which takes in a function $f$ and outputs the function that represents composing $f$ for a total of $n$ times, namely $f^{\circ n} = \underbrace{f \circ f \circ \cdots \circ f}_{n\text{ times}}$.&lt;/p&gt;

&lt;p&gt;So, zero simply takes $f$ to the identity function. One takes $f$ to itself. Two takes $f$ to $f \circ f$, and so on.&lt;/p&gt;

&lt;p&gt;Then, the given definitions make sense. To add one to a number $n$ (which is a function), you create a new number (function) that maps each function $f$ to $f^{\circ (n+1)}$, which can be computed as $f \circ f^{\circ n}$.&lt;/p&gt;

&lt;p&gt;To add $a$ and $b$ together, you create a new number (function) that maps each function $f$ to $f^{\circ(a+b)}$ which can be computed as $f^{\circ a} \circ f^{\circ b}$.&lt;/p&gt;

&lt;p&gt;This is really powerful. You might have heard of Peano axioms. This feels like actually implementing those axioms&lt;sup id=&quot;fnref:recursiveaxioms&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:recursiveaxioms&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;12&lt;/a&gt;&lt;/sup&gt;, just like implementing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons&lt;/code&gt; earlier.&lt;/p&gt;

&lt;p&gt;The Python code here illustrates the idea a bit more clearly, without using lambda expressions:&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;zero&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;identity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;identity&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;old_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wrapped&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;old_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrapped&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_number&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# so we have
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wrapped&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrapped&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_number&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wrapped&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrapped&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wrapped&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrapped&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;five&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wrapped&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrapped&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;infinite-streams-are-trippy&quot;&gt;Infinite streams are trippy&lt;/h2&gt;

&lt;p&gt;The third chapter of the book focuses on the notion of states and mutability.&lt;/p&gt;

&lt;p&gt;Interestingly, the book (and I guess, programmers in FP languages) considers mutability to be complicated: Mutability force us to think about &lt;em&gt;time&lt;/em&gt; and reason through expression evaluations in a rather mechanistic manner.&lt;/p&gt;

&lt;p&gt;(In a lot of introductory programming courses, variables and mutations are among the first few things taught. This really makes me think that maybe imperative programming isn’t as intuitive as it seems to be. How would the programmer world be like if we teach functional before imperative?)&lt;/p&gt;

&lt;p&gt;To cope with this complexity, the book introduces us to streams. Instead of focusing on a value of a variable and seeing it as changing, we consider the function of time representing the trajectory of that variable instead, which is unchanging.&lt;/p&gt;

&lt;p&gt;Streams in Scheme are like linked lists but they are lazily evaluated and memoized. Specifically,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(cons-stream a b)&lt;/code&gt; is a &lt;em&gt;special form&lt;/em&gt; that gives a pair containing evaluated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; and a promise to evaluate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; at some point&lt;sup id=&quot;fnref:promise&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:promise&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;13&lt;/a&gt;&lt;/sup&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(stream-car x)&lt;/code&gt; accesses the first element of the pair &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; as usual.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(stream-cdr x)&lt;/code&gt; forces the evaluation of the second element, caches it, and returns it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can have infinitely long streams. For example, this code generates a stream of positive integers, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integers&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integers-starting-from&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons-stream&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;
               &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integers-starting-from&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;integers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integers-starting-from&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This works because the second element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integers&lt;/code&gt;, namely &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(integers-starting-from (+ n 1))&lt;/code&gt; has not been evaluated yet. So, we aren’t actually storing infinite amount of data in memory.&lt;/p&gt;

&lt;p&gt;Only when we try to access the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt;-th element are we forcing the evaluation of all the necessary elements. For example, if we try to access the third element, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(stream-car (stream-cdr (stream-cdr integers)))&lt;/code&gt;, we will have the following expansion:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-car&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-cdr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-cdr&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;integers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-car&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-cdr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-cdr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integers-starting-from&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-car&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-cdr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-cdr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons-stream&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integers-starting-from&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))))&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-car&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-cdr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integers-starting-from&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-car&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-cdr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons-stream&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integers-starting-from&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-car&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integers-starting-from&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-car&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons-stream&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integers-starting-from&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Other than that, we can treat streams as if they are lists.&lt;/p&gt;

&lt;p&gt;Most list functions have a stream equivalent you can use.
For example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stream-filter&lt;/code&gt; lazily constructs a new stream from a given stream which satisfies the predicate. Accessing the fourth element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(stream-filter (lambda (x) (divisible? x 2)) integers)&lt;/code&gt; (a stream of even numbers) will trigger the evaluation of the first eight elements of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integers&lt;/code&gt;.
The function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stream-map&lt;/code&gt; lazily calls the given function on each element of the given stream to get a new stream: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(stream-map (lambda (x) (* x x)) integers)&lt;/code&gt; creates a stream of perfect squares.&lt;/p&gt;

&lt;p&gt;This is different from iterators in Python. In Python, you have an iterator object which stores where you are and how it will continue computing the next values. You must call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;next&lt;/code&gt; every time to access each new element (or use Python’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; loop which is a syntactic sugar), and you can’t go back without creating a new iterator which may require recomputing certain expressions.&lt;/p&gt;

&lt;p&gt;If Python’s iterators &lt;em&gt;do&lt;/em&gt;, Scheme’s streams &lt;em&gt;are&lt;/em&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Infinite streams can do some really trippy things. Consider this:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ones&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons-stream&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;We are defining &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ones&lt;/code&gt; in terms of itself: The sequence &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ones&lt;/code&gt; consists of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;, followed by all elements of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ones&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This works perfectly because when we try to access the second element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ones&lt;/code&gt;, it evaluates to the first element of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ones&lt;/code&gt; which is known to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;. 
When we try to access the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt;-th element, it evaluates to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(i-1)&lt;/code&gt;-th element which can be computed (or memoized), and we eventually get to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, if we have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(add-streams a b)&lt;/code&gt; for lazily adding two streams &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; position-wise, we could do something like this:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;integers&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons-stream&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;add-streams&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ones&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;intgers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Then, the second element is simply the first element plus one, which is two. The third element is the second element (which we already know) plus one, which gives three, and so on.&lt;/p&gt;

&lt;p&gt;Similarly, here’s how we can define a stream of Fibonacci numbers:&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;fibs&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons-stream&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
               &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons-stream&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
                            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;add-streams&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-cdr&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;fibs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                                         &lt;span class=&quot;nv&quot;&gt;fibs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;In a way, this is like writing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib[n] = fib[n-1] + fib[n-2]&lt;/code&gt; and the base cases &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib[0] = 0&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib[1] = 1&lt;/code&gt; directly.
We don’t have to write a function that can compute the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;-th Fibonacci for us.
We just write what &lt;em&gt;all&lt;/em&gt; Fibonacci numbers are, and let lazy evaluation do the magic. (We also get memoization for free!)&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Here’s another cool example.&lt;/p&gt;
&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;primes&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons-stream&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
               &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-filter&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;prime?&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integers-starting-from&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;prime?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;iter&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;cond&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;square&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-car&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;divisible?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-car&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;iter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-cdr&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))))&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;iter&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;primes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;primes&lt;/code&gt; to be a stream of primes, which consists of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt; and other integers that satisfy the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prime?&lt;/code&gt; predicate&lt;sup id=&quot;fnref:efficientprime&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:efficientprime&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;14&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;But wait, the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prime?&lt;/code&gt; is actually defined in terms of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;primes&lt;/code&gt;! It checks whether &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; is divisible by any other prime numbers up to its square root.&lt;/p&gt;

&lt;p&gt;This recursive definition works because at any point in the generation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;primes&lt;/code&gt;, enough smaller numbers have been generated so &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prime?&lt;/code&gt; computations can succeed.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;This final example blew my mind with its simplicity.&lt;/p&gt;

&lt;div class=&quot;language-scheme highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;; (a0, a1, a2, a3, ...) represents the power series a0 + a1*x + a2*x^2 + a3*x^3 + ...&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; stream-map can take more than one streams at a time and combine the elements position-wise (here using a divide function)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integrate-series&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; constant coefficient omitted&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-map&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;integers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; 

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;exp-series&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; integral of e^x is e^x&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons-stream&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integrate-series&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;exp-series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;cosine-series&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; cos x = integral of -sin x with constant of integration 1&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons-stream&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;scale-stream&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integrate-series&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sine-series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sine-series&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;; sin x = integral of cos x with constant of integration 0&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cons-stream&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;integrate-series&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;cosine-series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-take&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;exp-series&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; outputs (1 1 1/2 1/6 1/24 1/120 1/720 1/5040 1/40320 1/362880)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-take&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;cosine-series&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; outputs (1 0 -1/2 0 1/24 0 -1/720 0 1/40320 0)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stream-take&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sine-series&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;; outputs (0 1 0 -1/6 0 1/120 0 -1/5040 0 1/362880)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A few things to notice: We are defining $\exp$ in terms of itself, integrated. We define $\cos$ in terms of the integral of $\sin$ and $\sin$ in terms of the integral of $\cos$.&lt;/p&gt;

&lt;p&gt;It seems like we’ve only written down the properties of $\exp$, $\sin$, and $\cos$, not how to compute them.
Yet, if we reason through this like in the previous examples, these properties already describe unambiguously how to compute each term.&lt;/p&gt;

&lt;p&gt;It’s a dance between streams. To compute the fourth term of cosine series, you need to compute the integral of the third non-constant term of sine, flipping the sign. The integral of the third term is simply the third term of sine scaled. To know the third term of sine, you need to look at the second term of cosine, which can be deduced from the first term of sine, and eventually, we’re back at the base case.&lt;/p&gt;

&lt;p&gt;I don’t think it’s the programming that’s interesting per se. Rather, I haven’t seen this way of recursively computing power series before.&lt;/p&gt;

&lt;p&gt;This is magical. Now I know why people talk so much about functional programming and why lazy evaluation seems to be such a big deal.&lt;/p&gt;

&lt;p&gt;(Try implementing those examples in Python. It’s a lot more annoying than you’d expect.&lt;sup id=&quot;fnref:ugly&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:ugly&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;15&lt;/a&gt;&lt;/sup&gt;)&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;That’s it! I’ve only finished the first three chapters so far. The last two chapters focus on interpretation and compilation, which I might work through at some point. SICP is not a functional programming book per se, and I’ve barely scratched the surface, but dang, this is cool. Not to mention, this is still only the “mostly mathematical” side of things. There are still practical Lisp languages to learn, like &lt;a href=&quot;https://gigamonkeys.com/book/&quot;&gt;Common Lisp&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you’re looking for something to read, SICP is a good choice.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:py&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;6.009 Fundamentals of Programming &lt;a href=&quot;#fnref:py&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:lexical&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;This is called lexical scoping. I won’t describe it in details here. &lt;a href=&quot;#fnref:lexical&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:functionshorthand&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;There is actually a shorthand for defining functions, which is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(define (&amp;lt;funcname&amp;gt; &amp;lt;arg1&amp;gt; &amp;lt;arg2&amp;gt; &amp;lt;...&amp;gt;) &amp;lt;body&amp;gt;)&lt;/code&gt;. &lt;a href=&quot;#fnref:functionshorthand&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:defmacro&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;This example was taken from &lt;a href=&quot;https://www.defmacro.org/ramblings/lisp.html&quot;&gt;https://www.defmacro.org/ramblings/lisp.html&lt;/a&gt;. &lt;a href=&quot;#fnref:defmacro&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:hygiene&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Note that the example given here isn’t very accurate of how macros are actually implemented. For one, note that the name &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generated-loop-n&lt;/code&gt; might be confused with user’s own &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generated-loop-n&lt;/code&gt;. &lt;a href=&quot;#fnref:hygiene&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:lambdasub&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;A lot of the macros can be implemented with higher-order functions. However, that is much less powerful in two ways. One, the programmer needs to be aware that he must pass in a lambda expression instead of being able to use the macro as if it is part of the language’s syntax. Two, the code is not expanded at compile time. Some macros can be really helpful in optimizing performance. &lt;a href=&quot;#fnref:lambdasub&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:extrapolate&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;SICP did not explicitly teach how to write macros. However, they really emphasized the idea that “code is data” in chapter 4 where we have to write a metacircular evaluator (an evaluator for Scheme that is written in Scheme itself). &lt;a href=&quot;#fnref:extrapolate&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:badoop&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;OOP can be taught really badly. It is possible to learn about OOP concepts and fail to understand what they are for at all. (See: exploiting inheritance to model an animal kingdom.) Some write object-oriented code that is so contrived and intertwined, OOP turns from a tool for managing complexity into a tool for &lt;em&gt;adding&lt;/em&gt; unneeded complexity.) &lt;a href=&quot;#fnref:badoop&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:carcdr&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;There are some historical significances to these names you can probably Google. Some Lisp dialects change these to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rest&lt;/code&gt;. &lt;a href=&quot;#fnref:carcdr&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:acceptmodel&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Though, this requires us to accept the model of evaluation used in Scheme, and more generally, lambda calculus. &lt;a href=&quot;#fnref:acceptmodel&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:lambdavideo&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;My friend recommended a really good talk introducing lambda calculus: &lt;a href=&quot;https://www.youtube.com/watch?v=3VQ382QG-y4&quot;&gt;https://www.youtube.com/watch?v=3VQ382QG-y4&lt;/a&gt;. &lt;a href=&quot;#fnref:lambdavideo&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:recursiveaxioms&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Of course, that means your axioms are now lambda calculus instead. I guess I should learn set theory at some point. &lt;a href=&quot;#fnref:recursiveaxioms&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:promise&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;We can implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cons-stream&lt;/code&gt; as a macro that transforms &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt; into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(lambda () b)&lt;/code&gt;, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(stream-cdr x)&lt;/code&gt; is simply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;((cdr x))&lt;/code&gt; (take the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cdr&lt;/code&gt; then call to force the evaluation). (Don’t forget to add memoization too.) &lt;a href=&quot;#fnref:promise&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:efficientprime&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;There are much more efficient ways to compute primes, like the &lt;a href=&quot;https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Incremental_sieve&quot;&gt;incremental sieve&lt;/a&gt;. &lt;a href=&quot;#fnref:efficientprime&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:ugly&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Rather, you’ll see a lot of lambdas that just don’t belong there aesthetically. &lt;a href=&quot;#fnref:ugly&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Thu, 24 Jun 2021 05:50:00 -0400</pubDate>
        <link>https://tcpc.me//2021/06/24/sicp-is-cool.html</link>
        <guid isPermaLink="true">https://tcpc.me//2021/06/24/sicp-is-cool.html</guid>
        
        <category>Summer</category>
        
        <category>Books</category>
        
        <category>Reading logs</category>
        
        <category>Programming</category>
        
        
      </item>
    
      <item>
        <title>Fall 2020 Reflections</title>
        <description>&lt;p&gt;&lt;a href=&quot;/2020/10/05/fall-term-first-month.html&quot;&gt;Last time&lt;/a&gt;, I wrote about my first-month impression of the COVID-infested Fall semester. It has been three months since then, and I’m honestly surprised at how fast it was. Yet, simultaneously, each week felt painfully slow. I guess this is what happens when there are a lot of deadlines and your brain can only handle so many at once.&lt;/p&gt;

&lt;p&gt;Enough rambling! It’s finally the end of the semester (and more importantly, &lt;a href=&quot;https://is2020over.com/&quot;&gt;the end of 2020&lt;/a&gt;) which means I get to write a &lt;em&gt;real&lt;/em&gt; end-of-semester review. Because I talked so much about my thoughts on classes last post, I don’t really have much more to say except the things that changed (for better or worse). There have been a lot of developments during the semester, though, including some of the more personal ones. I’ve also collected some juicy statistics. So, that’s what we’re going to talk about.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;how-to-sleep-like-a-normal-human&quot;&gt;How to sleep like a normal human&lt;/h2&gt;

&lt;p&gt;Remember the sleep log graph I posted last time? If you don’t (or haven’t seen it), here’s a summary: I slept during daytime. At first, it was something like 9 AM to 5 PM, then it kept shifting forward. The duration also varied drastically between as low as 3 hours a day and 13 hours a day.&lt;/p&gt;

&lt;p&gt;Part of the issue was timezone misalignment: I had classes to take between 9 PM and 2 AM some nights. However, the lack of discipline also played a role. My body clock being severely misaligned with sunlight also made it worse. And quite frankly, when you have a schedule like that, you can’t even decide on &lt;em&gt;when&lt;/em&gt; you want to sleep, because none of the choices make any sense.&lt;/p&gt;

&lt;p&gt;Every once in a while I would have the motivation to fix it, and so I did. I was able to sleep at 1 AM like some other people for a while. And then it would be 4 AM. Before I knew it, I was back to sleeping at 3 PM, sometimes even skipping sleep. This cycle happened a few times.&lt;/p&gt;

&lt;p&gt;If you know me personally, you were most definitely concerned and probably told me many times to go to sleep. If you don’t know me personally and read the last blog, you were probably also concerned. If you don’t know me personally and just learned about this, you’re &lt;em&gt;now&lt;/em&gt; probably concerned too.&lt;/p&gt;

&lt;p&gt;Today I have good news. Look at this:&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-reflections/sleeptime.png&quot; alt=&quot;&quot; width=&quot;80%&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;Top: When I slept. Bottom: Total duration of sleep ending on that day. &lt;br /&gt;
(Credits to &lt;a href=&quot;https://rayaburong.github.io/&quot;&gt;@arunwpm&lt;/a&gt; for providing me the visualization!)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Miraculously, I fixed my sleep schedule &lt;em&gt;and&lt;/em&gt; managed to maintain it, not just for a week, but for a whole month. For the first time ever, when people ask me when I usually sleep, I have a definite answer. It’s 3:30 AM till noon. (Note that’s 8 hours!)&lt;/p&gt;

&lt;p&gt;Here’s my theory on why I managed to fix it. From the graph, my sleep schedule was decaying extremely quickly in early November. It got so bad that I basically circled back to the “normal” sleeping times. That coincided with when I was motivated to fix my sleep schedule. So, it was natural for me to maintain it before I would sleep at, say, 8 AM again. I was still failing, though.&lt;/p&gt;

&lt;p&gt;Then, November 20th, thanksgiving break started. Coincidentally, that was one week before Thailand Olympiad in Informatics, which meant there would be a training camp at a nearby university (about two hours away). As hosed as I was, I decided to commit to tutoring, hoping it would help distract me from all the school work I would have to deal with after the break.&lt;/p&gt;

&lt;p&gt;That turned out to be a fantastic decision. I was forced to wake up consistently before 8 AM to get my breakfast (a meal I’d never had in a really long while) and lead my students from wherever we were sleeping to the university. That was a solid 20-minute walk, plus another 20 minutes back. Factor in my activity during the day then that week was probably the most active I’ve ever been in the past eight months. (Imagine having an average of only a few hundred steps logged on your phone every day, and then suddenly getting almost 10k steps/day for a week.) Add the responsibility to wake up on time every day, and there you go, a recipe for fixing my messed up sleep schedule and not being sedentary.&lt;/p&gt;

&lt;p&gt;The camp was rather reinvigorating, so I had the motivation to somewhat maintain the schedule as I came back home. I’ve been getting loads of encouragement, and having someone hold me accountable works amazingly well. (Thank you so much! &amp;lt;3) I can’t say with absolute certainty this will last forever, but since I already got into the rhythm of forcing myself to sleep and wake up at the right time, I’m pretty optimistic this will keep going.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;being-hosed-101&quot;&gt;Being hosed 101&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;hosed&lt;/em&gt; (adj.) - flooded with work, as if attempting to drink from a firehose&lt;sup id=&quot;fnref:source&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:source&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At the beginning, I was curious how time-consuming MIT would actually be. So, I decided to set up Notion&lt;sup id=&quot;fnref:notion&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:notion&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; to track my time. After a full semester of collecting data, I got a pretty revealing breakdown of how intense each class was. Take a guess at how bad things were. Recall, I took:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;3.091 Introduction to Solid-State Chemistry (GIR&lt;sup id=&quot;fnref:gir&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:gir&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;)&lt;/li&gt;
  &lt;li&gt;6.036 Introducton to Machine Learning&lt;/li&gt;
  &lt;li&gt;6.854 Advanced Algorithms&lt;/li&gt;
  &lt;li&gt;21M.011 Introduction to Western Music (CI-H&lt;sup id=&quot;fnref:cih&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:cih&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s the big reveal:&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-reflections/circlechart.png&quot; alt=&quot;&quot; width=&quot;80%&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;Time spent on academics in Fall 2020, broken down by classes.
Note that dropped classes include 6.046 Design and Analysis of Algorithms (did the first two psets) and a bunch of other discovery classes I tried for the first three weeks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As it turns out, 6.854 actually took the majority of my time, which was kinda expected. I recorded a total of 256 hours&lt;sup id=&quot;fnref:nice&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:nice&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;5&lt;/a&gt;&lt;/sup&gt; which is just a little less than 20 hours per week. I don’t think it tells the full story of how intense the class actually is, though. I couldn’t predict the difficulty of the psets, so the added anxiety made the class feel like it actually took 30 hours per week. In hindsight, I should’ve spent less time worrying and more actually working on the psets. Also, lesson learned: asking for help does significantly cut down the amount of time. I wouldn’t have survived Pset 9 if not for kind folks who were willing to explain the same thing over and over to me. I’m forever grateful for your help. 🙏&lt;/p&gt;

&lt;p&gt;6.854 also has a final project where you have to do one of these:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Pick a few research papers you find interesting and summarize and synthesize them. The goal is to present the concepts with most clarity and perhaps present new connections between those papers. The papers should be relatively new (i.e. published in the last 3-4 years).&lt;/li&gt;
  &lt;li&gt;Pick a paper (or more) containing new algorithms. Implement them to try and figure out their performances on actual machines by designing some test sets. Perhaps find optimizations that help the algorithms work faster in practice.&lt;/li&gt;
  &lt;li&gt;Do some original research.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From the very beginning, I planned to do the reading project. Even Karger himself recognized it as the most straightforward way to get an A. There’s one problem: The project was released super late into the semester, with proposal due right after Thanksgiving break and the project itself due the week after. We only got the info and the old, sample projects a few days before the Drop date (for dropping classes or changing grading system). The level of expectation came as a big surprise, and the time alloted for it was barely enough. Needless to say, the last few weeks of the semester were stressful.&lt;/p&gt;

&lt;p&gt;Surprise! There was also a mandatory peer review/editing session two days before the project due date. I freaked out. Luckily, it only added about 1.5 hours, but this was the tipping point. I wanted to recommend 6.854 to people who love algorithms but now I realize there needs to be an &lt;em&gt;enormous&lt;/em&gt; warning tag. Karger is an amazing professor who gives some of the best lectures I’ve had. He just needs serious help organizing stuff&lt;sup id=&quot;fnref:badcourses&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:badcourses&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;6&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;So, this is what ended up happening: We looked for papers that seem promising for a few hours, chose them, and sent in project proposal on Monday (Dec 5). After two days, one follow-up email, and five other assignments I did while waiting, we finally got an approval. We only got to start working on the project on Saturday because, well, it’s the final stretch; things get busy (understatement). The project was grueling because the paper we chose was rather badly written. Slogging through&lt;sup id=&quot;fnref:unhealthy&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:unhealthy&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;7&lt;/a&gt;&lt;/sup&gt;, we managed to finish it on Wednesday night, right on the due date&lt;sup id=&quot;fnref:extend&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:extend&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;8&lt;/a&gt;&lt;/sup&gt; (Dec 9). I don’t know how to explain the relief.&lt;/p&gt;

&lt;p&gt;Here’s the breakdown of time I spent on classes each week as we’re moving away from 6.854:&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-reflections/byweek.png&quot; alt=&quot;&quot; width=&quot;90%&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;Time spent on academics in Fall 2020, broken down by week. Note that the first week (with no psets) count as week 0. Thanksgiving and final weeks are merged into adjacent weeks. For smaller assignments, I defaulted to recording on the due week. For larger assignments, I tried to be as accurate as I could be.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Surprisingly&lt;sup id=&quot;fnref:tomany&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:tomany&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;9&lt;/a&gt;&lt;/sup&gt;, 21M.011 took substantially more time than 6.036. I spent a total of ~80 hours, which is about 6 hours per week. On normal weeks, it’s actually about 3-4 hours. That’s 3 hours of a mandatory asynchronous lecture and two live recitations. Add another hour if you have a quiz that week and need to prepare for it&lt;sup id=&quot;fnref:read&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:read&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;10&lt;/a&gt;&lt;/sup&gt;. The rest of the hours were thanks to the essays. 21M.011 is an &lt;em&gt;amazing&lt;/em&gt; class with truly fantastic staff. (Emily Pollock and Teresa Neff are the best!) The workload is perhaps among the lightest of all HASS classes&lt;sup id=&quot;fnref:hottake&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:hottake&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;11&lt;/a&gt;&lt;/sup&gt;, so please consider 21M.011 if you’re still looking for a fun CI-H&lt;sup id=&quot;fnref:essays&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:essays&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;12&lt;/a&gt;&lt;/sup&gt;!&lt;/p&gt;

&lt;p&gt;6.036 had 60 hours recorded, or about 4.5 hours per week. 6.036 was perhaps the most consistent class in my schedule, mainly because there were no midterms or final exams and homework take roughly two hours every time. Add two hours of weekly labs plus exercises plus nanoquizzes. Then maybe add an hour or five if the god of bugs are in a bad mood or you want to go down the matrix calculus rabbit hole.&lt;/p&gt;

&lt;p&gt;Finally, we get to 3.091. I actually spent less and less time on 3.091 each week. Learning the homework solutions and skimming the textbook turned out to be enough for getting a decent grade, especially since all quizzes and exams were open-book this semester. I also hate organic chemistry with passion. Considering 3.091 publishes grade cutoffs at the beginning of the term and freshman fall grades don’t actually show up on transcripts, I decided to go into the final exam without any preparation&lt;sup id=&quot;fnref:finalexam&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:finalexam&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;13&lt;/a&gt;&lt;/sup&gt;. I ended up recording only 43 hours total, averaging at 3-ish hours per week.&lt;/p&gt;

&lt;p&gt;I recorded a total of 466 hours on classes this semester (including the dropped ones) with the median of 33 hours on a single week. I probably spent like an hour or two more every week if you counted the time it took for me to plan out my assignments, manage things on Notion, and do other administrative stuff. It amazes me just how much I’ve learned in a semester, even though it’s just through four classes. (Oh! I also just &lt;a href=&quot;/projects/6854-lecture-notes.html&quot;&gt;released my lecture notes for 6.854&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;I certainly wasn’t consistent. Instead of working 6 hours/day, it was more like 2 hours/day and one spike for 6.854 every week. Yet, I still managed to feel as tired as some other people I know spent 40-50 hours per week on classes. I think it just shows that there’s a huge mental component to how draining things get. If there’s anything I’ve learned from this semester, it would be to follow my energy patterns more. (If you know you’re not gonna start the pset until two days before the deadline, why bother agonizing over it for the entire week every week?) Ironically, committing to more things in a day also helped me feel better. It makes sense: When you’re stressed out about work, the best thing isn’t to do nothing and worry about not making progress. Hobbies like gaming or writing (which might seem demanding) are actually a pretty good escape from intense college life.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-reflections/notionweek.png&quot; alt=&quot;&quot; width=&quot;80%&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;Notion is aesthetically pleasing, so I thought I should put the screenshot here to fill the space before the next section.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;social-media-detox&quot;&gt;Social Media Detox&lt;/h2&gt;

&lt;p&gt;This is somewhat related to the previous section, and this is sort of a rant.&lt;/p&gt;

&lt;p&gt;I feel social media takes a lot of the time I could’ve used to finish my work or pursue other hobbies. Facebook, Twitter, Instagram, and Youtube are fine when there are genuinely interesting contents. But when you get to the point where you exhaust everything they have to offer for the day and you still feel the need to pick your phone up every few minutes to check if there’s been an update, that’s problematic.&lt;/p&gt;

&lt;p&gt;It’s like a toxic relationship: I know objectively I should quit yet for some reason I always find a way to rationalize sticking with it. For example, I tried to create an elaborate plan to ensure that I still don’t miss out on things while not wasting too much time. That never worked. In a way, social media has become a way for people to cope with not knowing what to do. They’ve evolved so much to capture people’s attention. It definitely worked on me.&lt;/p&gt;

&lt;p&gt;The short-term solution turns out to be quite simple: Shut everything down as soon as I realize I’m spending too much time on it. Bore myself by doing nothing. In four or five minutes, I’ll gain the clarity that allows me to start working on things long overdue. Of course, this doesn’t always work, but it has worked often enough to convince me that social media are indeed &lt;em&gt;designed&lt;/em&gt; to be addictive. &lt;a href=&quot;https://youtu.be/wf2VxeIm1no&quot;&gt;CGP Grey articulated this point very well in his video&lt;/a&gt; which I encourage you to watch when you can.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/wf2VxeIm1no&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;/blockquote&gt;

&lt;p&gt;Though the issue of social media addiction has been on my mind for a very long time, I’ve only been able to act this past mid-November. &lt;a href=&quot;https://www.youtube.com/watch?v=jm7ZAMAsPxI&quot;&gt;Some things happened&lt;/a&gt; that led me to think: What if I just go cold turkey? On the spur of the moment, I decided to delete all social media apps from my phone and downloaded an extension to block the websites. That worked quite well, especially since I was busy with final projects and training camps in November&lt;sup id=&quot;fnref:november&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:november&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;14&lt;/a&gt;&lt;/sup&gt; anyway.&lt;/p&gt;

&lt;p&gt;I gradually added back some stuff, but I think I’ve been using social media a lot more healthily than before. I’ve completely cut my ties with Instagram, though, because I realized it doesn’t actually add any entertainment value at all.&lt;/p&gt;

&lt;p&gt;The problem isn’t gone yet, though. Turns out there are infinite number of ways to procrastinate, including but not limited to, Discord, Piazza (yes, 6.AcAd is surprisingly fun), Firehose, Courseroad, and YouTube (which for some reason I haven’t removed yet). Guess this will go on for a while.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;iap-and-spring-semester-plans&quot;&gt;IAP and spring semester plans&lt;/h2&gt;

&lt;p&gt;Following the grueling Fall semester at MIT, there’s a one-month semester called the Independent Activities Period (IAP) which is meant to be a short but fun opportunity to do things. For example, you can take &lt;a href=&quot;http://weblab.mit.edu/&quot;&gt;web.lab&lt;/a&gt; which is simultaneously a crash course on web programming and a competition (with prize pools!) or &lt;a href=&quot;http://battlecode.org/&quot;&gt;Battlecode&lt;/a&gt; which teaches you to write bots that compete in a real-time strategy game with other people’s bots. There are also some of the more conventional classes, like Calculus (18.02A, extended from Fall semester), &lt;a href=&quot;https://ocw.mit.edu/courses/physics/8-223-classical-mechanics-ii-january-iap-2017/&quot;&gt;Classical Mechanics II&lt;/a&gt;, accelerated language classes, and other special topics in math/computer science/etc. IAP is also an opportunity for students to focus on doing research and other &lt;em&gt;activities&lt;/em&gt; (as the name suggested). It’s also possible to simply use IAP as an extended winter break before the spring term starts again.&lt;/p&gt;

&lt;p&gt;I guess this means there will be another review blog in a month! I’m planning to take &lt;a href=&quot;https://pokerbots.org/&quot;&gt;Pokerbots Competition&lt;/a&gt; and &lt;a href=&quot;http://introtodeeplearning.com/&quot;&gt;6.S191 Introduction to Deep Learning&lt;/a&gt;. The former just seems fun, yet light enough to offset some of the commitments I have outside of school. The latter, I assume, is a somewhat application-focused introduction to machine learning. I’ve already learned a lot from 6.036 but I haven’t had any experience working with libraries yet, so I figure 6.S191 would be a pretty enlightening experience.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-reflections/obama.png&quot; alt=&quot;&quot; width=&quot;80%&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;Obama was “invited” to introduce people to 6.S191. Of course, the audio and video were actually synthesized. Not sure how good 6.S191 will actually be, but starting with this is pretty cool I guess.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;IAP classes are known to be very intense, but they are graded P/D/F (pass/D/fail), and for freshmen specifically, P/NR (pass/no record). So, the pressure will be quite low regardless. Furthermore, I’m quite lucky Pokerbots lectures are scheduled during the first half of IAP meanwhile 6.S191 is in the second half, so that’ll help spread out the workload. If everything fails, however, dropping a class is also an option. We’ll see how that goes.&lt;/p&gt;

&lt;p&gt;MIT actually invites all freshmen, sophomores, and juniors on campus this spring, albeit with some rather tight restrictions (e.g. COVID-19 tests two times a week, strict social distancing, almost all classes still virtual). I know a lot of people are going. Sadly, after weighing all the benefits and headache-inducing logistical issues, I’ve decided not to go on campus. This means I’ll pursue spring courseload entirely online and won’t get to meet MIT friends in person. Thankfully, I think I’ve handled fall semester well, so I can probably tough it out for another semester. I’ve come up with a lot of stuff to do as well, so it will still be somewhat exciting. I’m definitely going on campus in Fall 2021, though, unless the pandemic somehow worsens. I’m somewhat optimistic it can’t get much worse than what we have right now. But we’ll see!&lt;/p&gt;

&lt;p&gt;Happy end of the year. Hope 2021 will be a somewhat less exciting year!&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-reflections/mitwinter.jpg&quot; alt=&quot;&quot; width=&quot;90%&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;A random photo of MIT in the winter (&lt;a href=&quot;http://web.mit.edu/nnf/people/suraj/photos1.html&quot;&gt;source&lt;/a&gt;). There’s generally more snow than this.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:source&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Source. See &lt;a href=&quot;https://mitadmissions.org/blogs/entry/speaking_mitese&quot;&gt;Paul’s blog&lt;/a&gt; on MIT slangs (a pretty old one but still relevant). See &lt;a href=&quot;http://slugwiki.mit.edu/index.php/Hosed&quot;&gt;Slug Wiki&lt;/a&gt;. (Do people use this anymore?) &lt;a href=&quot;#fnref:source&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:notion&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;a href=&quot;https://www.notion.so/&quot;&gt;Notion&lt;/a&gt; is a very powerful productivity web app that allows pretty much anything you can think of: calendars, to-do list, journaling, etc. I set up a dashboard that allows me to keep track of my assignments and compile class information so I don’t have to hunt them down on Canvas and hundreds of other websites every time I need something. If you’re interested in learning to use Notion, I suggest &lt;a href=&quot;https://www.youtube.com/watch?v=m9S5I3pWz94&quot;&gt;Thomas Frank’s video&lt;/a&gt;. He explained the features of Notion pretty well. Though you most likely won’t use Notion the same way he does, I’m sure you’ll get a lot of ideas on how to create your own system. &lt;a href=&quot;#fnref:notion&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:gir&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;General Institute Requirement (GIR). Science GIRs include two math classes, two physics classes, one chemistry class, and one biology class. 3.091 is chemistry. &lt;a href=&quot;#fnref:gir&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:cih&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;CI-H: also a synonym for ``you have to write &lt;em&gt;a lot&lt;/em&gt; of essays.” &lt;a href=&quot;#fnref:cih&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:nice&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;256: 2 to the power of 8. Beautiful. &lt;a href=&quot;#fnref:nice&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:badcourses&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;I heard there are courses with worse organizations, though. Who knows if I’ll end up loving 6.854’s administration? &lt;a href=&quot;#fnref:badcourses&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:unhealthy&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Oh yea. It was definitely unhealthy. The total time (minus the proposal) came out to be ~30 hours, so it was about 6 hours a day on average. That might not seem like much, but remember this is 6 &lt;em&gt;recorded&lt;/em&gt; hours, when I was focused on actually reading the research and trying to write things. Add in a few more hours of simply dying inside and two whole weeks of not seeing sunlight. Brain fog is real. &lt;a href=&quot;#fnref:unhealthy&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:extend&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Karger was actually pretty lenient with extensions. Formally however, classes are not allowed to have anything due after the last day of class (i.e. the formal due date for the project). &lt;a href=&quot;#fnref:extend&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:tomany&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Surprising to many other people I know at least. I think this was actually completely expected. &lt;a href=&quot;#fnref:tomany&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:read&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;I only discovered after a few weeks in that I could ignore the readings and only read right before the listening quizzes. Oops. &lt;a href=&quot;#fnref:read&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:hottake&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Okay, this was a hottake, but at least it’s the impression I got after seeing people complain about 14.01’s readings (non-CI-H, mind you) and 24.900’s final paper. &lt;a href=&quot;#fnref:hottake&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:essays&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Additional info for 21M.011: There are three essays. The first and the last one are analytical essays of 1000 words each. The second one is 2100 words with revision cutting down to 1600 words and is more personal in nature. There are also three “concert reports.” In a normal semester, you would attend a concert and then write a 300-to-500-word reflection on it. This semester, we were allowed to watch recorded concerts from any online libraries. I don’t know what you think about this, but I thought this was the lightest essay workload I could ask for in a CI-H class. &lt;a href=&quot;#fnref:essays&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:finalexam&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Confession: I actually submitted a blank exam. I couldn’t bother. I learned later that 40% or so of the class also didn’t care about the final exam. Guess I shouldn’t feel guilty. &lt;a href=&quot;#fnref:finalexam&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:november&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Hmm, now that I think about it, a lot happened in November. The question is: &lt;em&gt;What&lt;/em&gt; really happened? &lt;a href=&quot;#fnref:november&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Tue, 29 Dec 2020 12:50:00 -0500</pubDate>
        <link>https://tcpc.me//2020/12/29/fall-term-reflections.html</link>
        <guid isPermaLink="true">https://tcpc.me//2020/12/29/fall-term-reflections.html</guid>
        
        <category>Review</category>
        
        <category>Studying</category>
        
        
      </item>
    
      <item>
        <title>MIT first month of classes review</title>
        <description>&lt;p&gt;This blog was originally named “first &lt;strong&gt;week&lt;/strong&gt; of classes review,” but all of you can probably guess what happened. I was thinking of whether I should postpone this until the first semester ended but figured I have too many things I want to say right now, so welcome to &lt;del&gt;a rant&lt;/del&gt; an MIT review! I’m covering everything starting ranging from orientation, classes, COVID-era plans, to my sleep log. Refer to the table of contents below for… contents.&lt;/p&gt;

&lt;blockquote class=&quot;infobox no_toc&quot;&gt;
  &lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;
&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#covid-19-and-orientation&quot; id=&quot;markdown-toc-covid-19-and-orientation&quot;&gt;COVID-19 and Orientation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pnr-and-the-plan-grad-classes-couldnt-be-that-bad-right&quot; id=&quot;markdown-toc-pnr-and-the-plan-grad-classes-couldnt-be-that-bad-right&quot;&gt;P/NR and THE Plan: “grad classes couldn’t be that bad, right?”&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#classes-review-and-critiques&quot; id=&quot;markdown-toc-classes-review-and-critiques&quot;&gt;Classes Review and Critiques&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#6036-introduction-to-machine-learning&quot; id=&quot;markdown-toc-6036-introduction-to-machine-learning&quot;&gt;6.036 Introduction to Machine Learning&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#6854-advanced-algorithms&quot; id=&quot;markdown-toc-6854-advanced-algorithms&quot;&gt;6.854 Advanced Algorithms&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#3091-introduction-to-solid-state-chemistry&quot; id=&quot;markdown-toc-3091-introduction-to-solid-state-chemistry&quot;&gt;3.091 Introduction to Solid-State Chemistry&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#21m011-introduction-to-western-music&quot; id=&quot;markdown-toc-21m011-introduction-to-western-music&quot;&gt;21M.011 Introduction to Western Music&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#honorable-mentions&quot; id=&quot;markdown-toc-honorable-mentions&quot;&gt;Honorable mentions&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#reflections-and-looking-forward-please-go-to-sleep&quot; id=&quot;markdown-toc-reflections-and-looking-forward-please-go-to-sleep&quot;&gt;Reflections and Looking Forward: “please go to sleep”&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;covid-19-and-orientation&quot;&gt;COVID-19 and Orientation&lt;/h2&gt;

&lt;p&gt;As you may know, COVID-19 is a thing. (Sad.) Instead of being on campus for my Freshman Fall, I’m at my home in scorching Lampang, Thailand, attending MIT completely virtually. This is quite a bummer, because it doesn’t feel like I’ve turned into a proper, independent college student but MIT has welcomed us incoming freshmen quite well.&lt;/p&gt;

&lt;p&gt;The orientation schedule at MIT was similar to what it would normally have been: one week of pre-orientation for fun “discovery” programs and socializing and another week of main orientation for even more events.&lt;/p&gt;

&lt;p&gt;While pre-orientation was supposed to be very energetic (because everyone would be in the honeymoon phase with MIT), this year’s was somewhat quiet. I didn’t sign up for any FPOPs (first-year pre-orientation programs), because they required several short essay responses and I didn’t want to relive the college apps season. Luckily, there were some open FPOPs, so I got to see some of the presentations (and quitted ten minutes in). I ultimately spent most of the pre-orientation week planning my classes and setting up Vim and LaTeX for “livetexing.” This turned out to be a worthy investment, not for that I could take notes every class (I stopped after the second week.) but for helping me with quickly writing up problem sets right before the deadline.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-first-month/livetex036.png&quot; alt=&quot;&quot; width=&quot;70%&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;Feast your eyes upon my beautiful 6.036 lecture notes sponsored by Evan Chen’s gorgeous LaTeX template. (I’m literally just repeating the MITx notes, though. Oof.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Orientation week was a lot more packed. I met my advisor and people in my advising seminar, registered for classes, attended more info sessions (Nah, I didn’t.), filled twenty or so interest forms for clubs I’m vaguely interested in, and joined daily orientation group meetings. I’m not a fan of Zoom calls, but considering how easy it is in this situation to stay in my bubble, I decided to just hop in and talk with people. (It was still kinda awkward, though.) Most of my socializing comes from Discord, though. Class of 2024 has a pretty wholesome &lt;del&gt;and cursed&lt;/del&gt; community, and I enjoy chatting there a lot. (This happens to be quite an understatement.)&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;pnr-and-the-plan-grad-classes-couldnt-be-that-bad-right&quot;&gt;P/NR and THE Plan: “grad classes couldn’t be that bad, right?”&lt;/h2&gt;

&lt;p&gt;The best thing about MIT is that it really wants its students to challenge themselves regardless of what their backgrounds are. This means the requirements are extremely flexible, and there are plenty of opportunities to place out of easier classes if you think you are ready for more advanced ones. Furthermore, freshmen are on “P/NR” grading system in the first semester, meaning their grades only show up as a “Pass” on the transcript, and if they fail, nothing shows up at all. Second semester is ABC/NR: ABC shows up, while D and F are not recorded. All of this is meant to help students adjust to the rigor of MIT and is very appreciated.&lt;/p&gt;

&lt;p&gt;In the next few paragraphs, I will prove beyond any shadow of doubt that I am, in fact, not an overachiever.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-first-month/classplan.png&quot; alt=&quot;&quot; width=&quot;50%&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;Prerequisite graph of classes that are either requirements or look interesting. (Not all of the classes listed here are classes I plan to take.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Traditionally, students majoring in 6-3 (computer science and engineering) must take two algorithms classes: 6.006 Introduction to Algorithms and 6.046 Design and Analysis of Algorithms. Before taking 6.006, they must take 6.042 Mathematics for Computer Science. Because this is MIT, however, there are students with strong math and CS background who choose to skip 6.042 and/or 6.046. My original plan was also to skip both and do 6.046 first semester.&lt;/p&gt;

&lt;p&gt;All was fine and good until I asked one of my friends what they are taking this semester:&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-first-month/854discord.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And so I checked. 6.854 Advanced Algorithms is a graduate-level class in algorithms. I checked the syllabus and found the class to be very interesting as it continues directly from my current knowledge in algorithms (from competitive programming), while 6.046 seems to repeat a lot of what I already know. 6.854 also does not have high-stakes exams but rather big weekly problem sets. I decided against it.&lt;/p&gt;

&lt;p&gt;And then two other people told me they were gonna take 6.854. I didn’t budge. Then…&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-first-month/854project.png&quot; alt=&quot;&quot; width=&quot;90%&quot; /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I didn’t think I was ready for 6.854 because I didn’t have enough practice writing proofs. 6.046 also has a lot of things I can learn from too.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-first-month/854illhelpthem.png&quot; alt=&quot;&quot; width=&quot;90%&quot; /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Peer pressure is a strange thing. &lt;em&gt;-proceeds to register for 6.854-&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Actually, I decided to also enroll in 6.046 as a “listener” (basically auditing) to gain access to the course materials. My plan was to take both 6.046 and 6.854 concurrently for the first two weeks, submitting the problem sets for both classes. Then, I would decide which one to drop when grades are out. Of course, this meant I had to do double the amount of work for the first two weeks, but it couldn’t be that bad, right? &lt;em&gt;-sounds of utter pain and agony-&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I eventually committed to 6.854, though, and I think I made the right decision. Thanks to people who were encouraging me to milk the hell out of P/NR!&lt;/p&gt;

&lt;!--
### Advanced standing exams

MIT&apos;s main requirement is called the GIRs (General Institute Requirements). The science portion requires two classes in calculus (18.01x, 18.02x), two in physics (8.01x, 8.02x), one in chemistry (3.091/5.11x), and one in biology (7.01x). While that may sound like a lot, it is possible to test out of all of them through the Advanced Standing Exams (ASEs). AP credits are also accepted for single-variable calculus (18.01) and mechanics (8.01). (All of those require a 5. In fact, for 8.01, you need to do well on both AP mechanics and E&amp;M exams.)

Ideally, I would be able to test out of all the GIRs and some of the more advanced math classes. However, being a realistic person that I was, I decided my plan was going to look like this:
- Use AP credits for 18.01 (single-variable calculus) and 8.01 (mechanics).
- Test out of 6.0001 (intro to programming) and 18.02 (multivariable calculus). Simple.
- Test out of 5.111 (chemistry). Requires a lot of review.
- Self-teach 8.02 (E&amp;M).
- Self-teach 18.03 (differential equations) and 18.06 (linear algebra).
(No, I&apos;m not going to take 7.012 ASE because I&apos;m not willing to read up biology myself.)

But then, it turned into this.
- Use AP credits for 18.01 (single-variable calculus) and 8.01 (mechanics).
- Test out of 6.0001 (intro to programming) and 18.02 (multivariable calculus). Simple.
- ~~Test out of 5.111 (chemistry). Requires a lot of review.~~ _ASE was canceled. I didn&apos;t study too, so that&apos;s good._
- ~~Self-teach 8.02 (E&amp;M).~~ _Got too lazy so it was too late._
- ~~Self-teach 18.03 (differential equations) and 18.06 (linear algebra).~~ _Canceled. Didn&apos;t study too._

So, I ended up deciding to take 3.091 (Introduction to Solid-state Chemistry) freshman fall instead. For 8.02, 18.03 and 18.06, I&apos;ll somehow find free time to study during the semester. (Heh, good luck with that.)
--&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;classes-review-and-critiques&quot;&gt;Classes Review and Critiques&lt;/h2&gt;

&lt;h3 id=&quot;6036-introduction-to-machine-learning&quot;&gt;6.036 Introduction to Machine Learning&lt;/h3&gt;

&lt;p&gt;6.036 was my first lecture of this strange semester, and it started at 8:30pm&lt;sup id=&quot;fnref:mittime&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:mittime&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; my time. Tamara Broderick, the lecturer, was superb, so kudos for giving a good first impression to the course! I chose this class because I wanted to gain some experience in the more “practical” fields than competitive programming, and 6.036 didn’t disappoint in terms of content. In the first month we dived right into the fundamentals of machine learning: linear classifier, feature selection, logistic regression, and gradient descents. After this, we’ll get into the more complex topics such as neural networks, reinforcement learning, state machines, and recommender systems. Although the content is quite theoretical, I think it provides a good foundation for understanding some of the more buzzword-y things people like to talk about and build their projects with.&lt;/p&gt;

&lt;p&gt;The way this course is organized is that each week we have a lecture on Tuesday, a lab session on Thursday (or Friday) which is largely conceptual, an exercise (pre-class reading) due before the lecture, a 30-minute “nanoquiz” due before the lab, and a big coding homework due before next week’s lab. Obviously, there are many components to keep track of but I think all of them complement each other well in delivering the content to the students. I’ve had a pretty good experience in my lab sessions. My LA (lab assistant/section leader) is very enthusiastic, does a great job teaching us, and is just overall friendly. I’m somewhat frustrated by the coding homework, though, as they sometimes feel like “code and run this algorithm manually ten times and wait for three minutes each and tell us what values you get.” (More info in the footnotes.&lt;sup id=&quot;fnref:homeworkcomplaint&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:homeworkcomplaint&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;)&lt;/p&gt;

&lt;p&gt;Regardless of that, I definitely would recommend this course to anyone looking for an introduction to machine learning concepts. Advanced students in CS looking for a more efficient and demanding course might, however, benefit more from self-teaching and working on real ML projects to prepare for grad-level 6.867.&lt;/p&gt;

&lt;p&gt;This class me takes about 4-5 hours per week on average (closer to the lower end). I spend about an hour on reading the content, skimming the lectures&lt;sup id=&quot;fnref:lecture&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:lecture&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; and doing the quizzes, another hour attending lab sessions, and 2-3 hours doing the homework. The homework time might increase this month as we dive into more difficult topics.&lt;/p&gt;

&lt;h3 id=&quot;6854-advanced-algorithms&quot;&gt;6.854 Advanced Algorithms&lt;/h3&gt;

&lt;blockquote class=&quot;quote&quot;&gt;
  &lt;p&gt;“Who would’ve thought that without keeping extra information you could still achieve good performance without even balancing your tree? This is what separates the truly great: a few of these lightning strikes of insight during your life are enough to get you into the textbooks.”&lt;/p&gt;

  &lt;p&gt;David R. Karger&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here it comes. Welcome to 6.854, a graduate-level class taught by none other than &lt;a href=&quot;https://en.wikipedia.org/wiki/David_Karger&quot;&gt;David R. Karger&lt;/a&gt; himself. Many curious competitive programmers or computer scientists may recognize him from “Karger’s algorithm” for finding a min cut or as the person who introduced the term “consistent hashing.” Here, the content waits for no one as it jumps right into Fibonacci heap in the first lecture, followed by the brilliant analyses of splay trees and a multitude of flow algorithms. We will also check out linear programming, approximation algorithms, and computational geometry later in the semester too.&lt;/p&gt;

&lt;p&gt;This is definitely a class I would recommend if you think 6.046 isn’t enough to satisfy your curiosity. I’ve known some of these from my competitive programming career but Karger’s emphasis on the timeline of algorithms research really makes me appreciate the field even more. Karger also hinted a bit at how various personalities advance the field in different ways&lt;sup id=&quot;fnref:roast&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:roast&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;. The ideas presented are sometimes just… genius. If he teaches you how to find an MST in linear time to put you in awe and doesn’t tell you who came up with it, look it up on Wikipedia. (Spoiler: It was Karger.)&lt;/p&gt;

&lt;p&gt;The structure is rather straightforward: 1.5-hour lectures on Mondays, Wednesdays, and Fridays with huge problem sets released and due every Wednesday. Sounds simple enough. Ends up taking me more time per week than all my other classes combined because Karger definitely didn’t teach us FKS scheme and say, “It only took a few years to figure out how to support insertion. I’ll probably put it on your pset next week.” Problems are pretty fun, though! Some of them are mainly proof-based, but a lot also feel like competitive programming. In total, I put in about 20 hours per week. Thanks to the friends taking this course with me for helping me survive!&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-first-month/karger.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;This lecture was actually from 2013. I just wanted to insert a picture of him but all his classes this year are just a camera pointing at a whiteboard.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;3091-introduction-to-solid-state-chemistry&quot;&gt;3.091 Introduction to Solid-State Chemistry&lt;/h3&gt;

&lt;p&gt;My original plan was to take 7.012 Introductory Biology this semester (Biology is part of the requirement.) but I decided on a whim to take 3.091 instead because 1) people said it would be fun and cover more interesting topics than in high school and 2) it wouldn’t have required weekly homework so it would take me less effort than if I were to study and test out of it next semester. Sadly, 3.091 has been rather disappointing.&lt;/p&gt;

&lt;p&gt;3.091’s grading is based entirely on weekly quizzes (20%) and monthly exams (4 times, 20% each). Other components—three recorded lectures a week, two synchronous recitations&lt;sup id=&quot;fnref:rec&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:rec&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;, and practice homework—are completely optional but are supposedly crucial to our learning. Prof. Sadoway seems like a genuinely caring person and he does a somewhat decent job at lecturing. However, the bulk of important knowledge is actually in the online textbook which happens to be painful to read and contains a lot of serious maths typographical errors. The lectures and the readings aren’t coordinated well, so the lectures can get very confusing. The homework, which is supposed to prepare us for the quizzes, asks us to solve problems that aren’t adequately explained before, and the quizzes turn into an incentive to just read and memorize how to do the homework every Friday. I’ve heard that some people have good recitations but mine’s quality is rather underwhelming: It’s a 45-minute session of “please help fill this powerpoint for me and you can leave” and telling us the problem solutions without explaining anything beyond what’s written. I heard a lot of good things about 3.091 before, so I think this year’s problem might be attributed to changing of the professor and transitioning to online format.&lt;/p&gt;

&lt;p&gt;I’ve been able to survive until now only because I have some background from high school and I can bandage the gaps in my knowledge through &lt;em&gt;outside&lt;/em&gt; resources. Average time spent per week is 4.5 hours. After the first midterm this week, 3.091 will dive into some of the less familiar solid-state chemistry which will hopefully help me stay engaged. If not, then god bless P/NR.&lt;/p&gt;

&lt;h3 id=&quot;21m011-introduction-to-western-music&quot;&gt;21M.011 Introduction to Western Music&lt;/h3&gt;

&lt;p&gt;As the name suggests, 21M.011 explores a wide range of western music starting from the medieval period to the modern era. We get to hear a lot of interesting music, develop listening skills, and learn the terminologies to communicate the features of the music. Although I’m not the kind of person to like “art history” classes, I find 21M.011 very enjoyable because I like music overall and being exposed to a lot of styles of music just feels somewhat fulfilling. 21M.011 conveniently satisfies the communication requirement and might even come in handy if I happen to want to do a music minor in the future&lt;sup id=&quot;fnref:minor&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:minor&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;6&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Each week we have an hour-long recorded lecture on Wednesday and two one-hour recitations on Tuesday and Thursday (or Monday and Friday). Emily Pollock is a fabulous lecturer who gives us a very well-organized perspective of how western music is being developed. She is very enthusiastic, and she puts in a lot of effort to foster a sense of community despite the class being held online. It’s very clear from her lectures and other communications that she cares about her students and is willing to support in any way she can. If there’s anything, she does speak very, very fast, though. My recitation, led by a very kind professor, Teresa Neff, is also fantastic!&lt;/p&gt;

&lt;p&gt;This class is designated “communication-intensive,” so this means I have to write essays for the class. (We have two 1000-word essays, one 2000-word essay with revision process, and three 300-500 personal reports.) However, other than the weeks with essays due, this class has been pretty light. We also have biweekly quizzes which ask us to guess the composers for three mysterious pieces and detail our reasoning process. Those are thrilling but doable with a bit of preparation. I spend about 3.5 hours per week on average (not counting the essays).&lt;/p&gt;

&lt;h3 id=&quot;honorable-mentions&quot;&gt;Honorable mentions&lt;/h3&gt;

&lt;p&gt;I actually signed up for a lot of other classes at first but I eventually dropped them because I got accepted into some other classes with lottery or simply lost interest. Also, freshmen are allowed to take up to only 48 units (~4 classes) plus 9 discovery units (~3 classes) anyway. Nevertheless, here are some classes worth mentioning, in no particular order:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;24.900 Introduction to Linguistics&lt;/strong&gt;: I had to attend because I was in the waitlist for this class and 21M.011, and oh boy, Adam Albright was simply amazing. The first greeting we got was him speaking entirely in Ubbi Dubbi and sending us off to figure out how the language worked. The way he presented things just made linguistics seem very fun. Who would’ve expected that “massafuckingchusetts vs. massachufuckingsetts” would become the main topic of the lecture? Though, I got accepted to 21M.011 first and some of the class works seem better suited for on-campus learning, so I’ll probably take this class some time later.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-first-month/albright.png&quot; alt=&quot;&quot; width=&quot;60%&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;Choose one.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;21M.080 Introduction to Music Technology&lt;/strong&gt;: Same as 24.900, I attended because I was waiting for my waitlist responses. The first lecture didn’t feature much but Ian Hattwick was an enthusiastic lecturer and I think I would really enjoy the course’s content. Might take this class later too.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;14.009 Economics &amp;amp; Society’s Toughest Problems&lt;/strong&gt;: This was a 3-unit discovery class I chose because the topics seemed very interesting. I thought it would be a good idea to be exposed to economics even though I may be a computer science major. Each week we had an hour-long lecture by guest experts on some problems in economics, ranging from corruption to tech industry monopolies. The workload was minimal: attend synchronous lectures, do the readings, write responses occasionally. Though, I dropped the class three weeks in because I had to clear out Tuesday for working on 6.854 homework.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-first-month/intltrade.png&quot; alt=&quot;&quot; width=&quot;60%&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;This slide in 14.009 does two things: 1) conveys some facts regarding international trade, and 2) makes people in &lt;del&gt;cursed&lt;/del&gt; Discord go crazy.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;15.000 Explorations in Management&lt;/strong&gt;: Also a 3-unit discovery class with minimal requirement. The first lecture was on the United States’s crazy expensive healthcare and it was fantastic. Unfortunately, the lectures were two hours long and I couldn’t afford to stay up until 9 AM for the class so I dropped out.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;24.A02 Science &amp;amp; Meaning of Life&lt;/strong&gt;: This was a 3-unit seminar tied to my advisor. Each week, we would read Alan Lightman’s book &lt;em&gt;The Accidental Universe: The World You Thought You Knew&lt;/em&gt; and discuss some philosophical stuff for an hour. While I found the book to be interesting, I didn’t enjoy the discussions as much as I did at Cranbrook, so I decided to drop. Timezone was also a big issue.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;6.046 Design and Analysis of Algorithms&lt;/strong&gt;: I did the first problem sets for this because I wasn’t confident I would survive in 6.854, so I thought I would document my pain here. The problem sets took me just about as much time as 6.854. (The later psets after I left were a lot easier, though.)&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;reflections-and-looking-forward-please-go-to-sleep&quot;&gt;Reflections and Looking Forward: “please go to sleep”&lt;/h2&gt;

&lt;p&gt;All this talk about attending Zoom University doesn’t mean anything until we talk about timezones!&lt;/p&gt;

&lt;p&gt;For context, I almost always get full 8-9 hours of sleep every day outside the semesters. The times are, however, not very consistent. I start my breaks with 2-10 am sleep schedule and try my best not to let it shift. Inevitably, I end up sleeping at 3 am, and 4, and 5, and it keeps going. My break’s usually over by that point so I don’t spiral any further.&lt;/p&gt;

&lt;p&gt;This year’s different because there’s no in-person college forcing me to sleep like a normal person. I thought my messed up schedule might work in my favor because I could just follow MIT time (11 hours apart), so I did what any sane person would do: sleep at 10 am and wake up at 6 pm. This would be equivalent of sleeping at 11 pm and waking up at 7 am Eastern time.&lt;/p&gt;

&lt;p&gt;And so I did. And I failed.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-first-month/sleeptime.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;My sleep time starting in September. The top plot marks when I slept. The bottom plot marks the total duration of sleep on that day. For sessions spanning two days, I put all the duration into the second day’s bar.
(Credits to &lt;a href=&quot;https://rayaburong.github.io/&quot;&gt;&lt;strong&gt;@arunwpm&lt;/strong&gt;&lt;/a&gt; for providing me the visualization!)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It turned out to be very hard to sleep when there’s sunlight. Plus, since my time matched other people’s, it was easy for me to just stay up late and chat with people, making my schedule even worse. By the fourth week, I didn’t even bother to sleep properly because the concept of time didn’t make sense anymore and I was just scrambling to finish my psets.&lt;/p&gt;

&lt;p&gt;Starting this week, I’m committing to a 2-10am schedule, a.k.a. the schedule that I should’ve followed in the first place. I don’t know why it just only makes sense to me now but this schedule actually works much better than, say, biphasic schedule I might’ve come up with. Here are some pros of 2-10am schedule:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;I get to sleep at nighttime, which I can confirm does feel a lot more restorative than daytime.&lt;/li&gt;
  &lt;li&gt;I still get to talk to people at 10am-2pm my time or 11pm-3am MIT time, which is somewhat late but still very possible because MIT students stay up late anyway.&lt;/li&gt;
  &lt;li&gt;Works well with my classes which are within the range of 8:30pm til 2:00am. (This is only possible because I dropped all three discovery classes.)&lt;/li&gt;
  &lt;li&gt;If there’s some important meeting, I can easily stay up later than 2am. As long as I still wake up at the same time or do not allow myself to go to sleep later than 2am the next day I should be fine.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;I guess that’s it! I’ve totally circled back to where I started at the beginning of September, albeit with more clarity than ever. I know how to deal with timezones. I have a record of how much time I might need to spend on each class. And perhaps, most importantly, I’ve come to accept what my schedule for working on 6.854 psets would be like. Hopefully, these knowledge will help me manage time better and allow me to relax/do other things outside of MIT classes.&lt;/p&gt;

&lt;p&gt;It’s still been one month. I can’t wait to see what surprise&lt;sup id=&quot;fnref:unpleasant&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:unpleasant&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;7&lt;/a&gt;&lt;/sup&gt; this semester has in store for the next three months.&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/fall-term-first-month/calendar.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;“This ride only stops in an emergency. Crying is not an emergency.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:mittime&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Actually 8:35pm because “MIT time” dictates that any respectable classes and events should start 5 minutes late and end 5 minutes early. &lt;a href=&quot;#fnref:mittime&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:homeworkcomplaint&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;The homework problems are broken into many small, more straightforward and digestible parts, so it does a pretty good job at holding the students’ hands. However, the questions can be downright annoying. For example, we had to compute by hand the result of some algorithms way too many times. Some of the “coding” problems asked us to wrap our head around their badly documented helper functions and run them repeatedly on a large data set just to illustrate that some parameters are better than the others. The homework is too mechanical as we can see many students asking the same questions for which the homework claims to have already helped build their intuition. I personally would love to see this class’s homework elaborate more upon the concepts we’ve learned in class by actually having the students code the algorithms and apply them to real projects, challenging their intuition and warning them of the very real mistakes they can make. Some of the existing homework problems (e.g. digit classification) already have this potential. They just have to stop asking to implement a function that takes in a Numpy array and spits out a column vector representing the sum of each row only to feed it into some mysterious helper function. &lt;a href=&quot;#fnref:homeworkcomplaint&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:lecture&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;I stopped attending lectures after the first one because it’s faster just to read the slides. Oops. &lt;a href=&quot;#fnref:lecture&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:roast&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Karger definitely didn’t roast his colleagues for using numerical methods for flow algorithms. &lt;a href=&quot;#fnref:roast&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:rec&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Recitations are like review sessions with the course’s upperclassmen TAs. &lt;a href=&quot;#fnref:rec&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:minor&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;A minor at MIT is surprisingly easy to get. For music minor, you need to take two introductory classes, one history, one theory, one performance, and one more advanced history/theory/performance. Since I am already planning to satisfy the mandatory HASS&lt;sup id=&quot;fnref:hass&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:hass&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;8&lt;/a&gt;&lt;/sup&gt; concentration requirement with three classes in music, that means I only have to take three more classes to get a full minor. It’s not very rare to see someone graduate with double majors and triple minors. (Don’t worry. I’m not doing that.) &lt;a href=&quot;#fnref:minor&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:unpleasant&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Probably some unpleasant ones but I’m optimistic. &lt;a href=&quot;#fnref:unpleasant&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:hass&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Humanities, Arts, and Social Science &lt;a href=&quot;#fnref:hass&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Sun, 04 Oct 2020 14:30:00 -0400</pubDate>
        <link>https://tcpc.me//2020/10/04/fall-term-first-month.html</link>
        <guid isPermaLink="true">https://tcpc.me//2020/10/04/fall-term-first-month.html</guid>
        
        <category>Review</category>
        
        <category>Studying</category>
        
        
      </item>
    
      <item>
        <title>ตะลุยโจทย์ Graph ระดับโหดใน Competitive Programming</title>
        <description>&lt;p&gt;ส่วนใหญ่แล้วโจทย์กราฟที่เราพบในการแข่งขันต่าง ๆ หรือพวกโจทย์สัมภาษณ์ มักจะเป็นโจทย์ที่ประยุกต์ใช้ Graph Algorithms บน input graph ตรง ๆ เช่น&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Depth-first Search/Breadth-first Search สำหรับท่องกราฟ&lt;/li&gt;
  &lt;li&gt;Dijkstra’s Algorithm สำหรับหา Single-source Shortest Path&lt;/li&gt;
  &lt;li&gt;Kruskal’s Algorithm สำหรับหา Minimum Spanning Tree&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;โจทย์บางข้ออาจจะซับซ้อนกว่านั้น เพราะ input ไม่ได้กำหนดกราฟมาให้ แต่เราต้องมองให้ออกเองว่าเราสามารถแปลงข้อมูลที่โจทย์ให้มาเป็นกราฟแล้วใช้พวก algorithm ที่มีอยู่แก้&lt;/p&gt;

&lt;p&gt;ในบทความนี้ ผมก็จะขอยกตัวอย่างโจทย์กราฟซับซ้อนบางข้อเพื่อเป็นแนวทางให้กับคนที่สนใจเข้าร่วมการแข่งขันอัลกอริทึมต่าง ๆ หรืออยากจะฝึกฝน problem-solving skill เพิ่มเติมครับ :)&lt;/p&gt;

&lt;blockquote class=&quot;infobox no_toc&quot;&gt;
  &lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;
&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#ข้อที่-1-knight-on-a-chessboard&quot; id=&quot;markdown-toc-ข้อที่-1-knight-on-a-chessboard&quot;&gt;ข้อที่ 1: Knight on a Chessboard&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#problem-statement&quot; id=&quot;markdown-toc-problem-statement&quot;&gt;Problem Statement&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#solution-idea&quot; id=&quot;markdown-toc-solution-idea&quot;&gt;Solution Idea&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#complexity-analysis&quot; id=&quot;markdown-toc-complexity-analysis&quot;&gt;Complexity Analysis&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ข้อที่-2-shortest-path-with-fee-waiver&quot; id=&quot;markdown-toc-ข้อที่-2-shortest-path-with-fee-waiver&quot;&gt;ข้อที่ 2: Shortest Path with Fee Waiver&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#problem-statement-1&quot; id=&quot;markdown-toc-problem-statement-1&quot;&gt;Problem Statement&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#solution-idea-1&quot; id=&quot;markdown-toc-solution-idea-1&quot;&gt;Solution Idea&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#complexity-analysis-1&quot; id=&quot;markdown-toc-complexity-analysis-1&quot;&gt;Complexity Analysis&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ข้อที่-3-hypergraph-traveling&quot; id=&quot;markdown-toc-ข้อที่-3-hypergraph-traveling&quot;&gt;ข้อที่ 3: Hypergraph Traveling&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#problem-statement-2&quot; id=&quot;markdown-toc-problem-statement-2&quot;&gt;Problem Statement&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#solution-idea-2&quot; id=&quot;markdown-toc-solution-idea-2&quot;&gt;Solution Idea&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#naive-approach&quot; id=&quot;markdown-toc-naive-approach&quot;&gt;Naive Approach&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#better-approach&quot; id=&quot;markdown-toc-better-approach&quot;&gt;Better Approach&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#complexity-analysis-2&quot; id=&quot;markdown-toc-complexity-analysis-2&quot;&gt;Complexity Analysis&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#บทสรุป&quot; id=&quot;markdown-toc-บทสรุป&quot;&gt;บทสรุป&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#recommended-problems&quot; id=&quot;markdown-toc-recommended-problems&quot;&gt;Recommended Problems&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;ข้อที่-1-knight-on-a-chessboard&quot;&gt;ข้อที่ 1: Knight on a Chessboard&lt;/h2&gt;

&lt;h3 id=&quot;problem-statement&quot;&gt;Problem Statement&lt;/h3&gt;

&lt;p&gt;สมมุติว่ามีตารางหมากรุกขนาด $n \times m$ แต่ละช่องของตารางอาจจะเป็นช่องปกติ หรือช่องที่ห้ามเข้าถึง&lt;/p&gt;

&lt;p&gt;เรามี Knight 1 ตัววางอยู่ที่ตำแหน่ง $(s_x, s_y)$ จงหาว่าเราสามารถเคลื่อนย้าย Knight ตัวนี้ไปยังตำแหน่ง $(t_x, t_y)$ ได้ด้วยการกระโดดน้อยสุดทั้งหมดกี่ครั้ง การกระโดดของ Knight เป็นดังภาพ&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/state-graph-tutorial/figure1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;รูปแบบการเดินของ Knight&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Knight ห้ามกระโดดออกนอกตาราง หรือกระโดดไปยังช่องที่ห้ามเข้าถึงเป็นอันขาด&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;solution-idea&quot;&gt;Solution Idea&lt;/h3&gt;

&lt;p&gt;หากเราสังเกตดี ๆ จะเห็นว่า เราสามารถมองว่าตารางที่กำหนดให้เป็นกราฟ โดยที่มีเส้นเชื่อมระหว่าง node ระบุถึงการเดินทางทั้งหมดที่เป็นไปได้ของ Knight ดังตัวอย่าง&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/state-graph-tutorial/figure2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;ตัวอย่างการสร้างกราฟจากตารางที่กำหนดให้&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;เมื่อเห็นเช่นนี้แล้ว การหาจำนวนครั้งกระโดดน้อยสุดจากตำแหน่ง $(s_x, s_y)$ ไปยัง $(t_x, t_y)$ ก็ไม่ต่างอะไรกับการหาเส้นทางสั้นสุดจาก node $(s_x, s_y)$ ไปยัง node $(t_x, t_y)$&lt;/p&gt;

&lt;p&gt;เนื่องจากเส้นเชื่อมทุกเส้นของกราฟมีน้ำหนักเท่ากัน (เพราะการกระโดดทุกแบบนับเป็นกระโดด 1 ครั้งเท่ากันหมด) เราจึงสามารถแก้โจทย์ดังกล่าวได้ด้วยอัลกอริทึม Breadth-first Search&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/state-graph-tutorial/figure3.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;การหาเส้นทางสั้นสุดจากกราฟที่สร้างขึ้น&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;complexity-analysis&quot;&gt;Complexity Analysis&lt;/h3&gt;

&lt;p&gt;ในกรณีแย่สุด BFS จะต้องท่องผ่านทุกจุดและทุกเส้นเชื่อม กราฟที่เรากำหนดขึ้นมามีจุดทั้งหมด $nm$ จุด และมีละจุดมีเส้นเชื่อมออกจากจุดนั้นประมาณ 8 เส้น จึงสรุปได้ว่าวิธีนี้มี Time Complexity และ Space Complexity เป็น $\mathcal{O}(nm)$&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href=&quot;https://ideone.com/Gbyjj7&quot;&gt;Solution Code 1&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;ข้อที่-2-shortest-path-with-fee-waiver&quot;&gt;ข้อที่ 2: Shortest Path with Fee Waiver&lt;/h2&gt;

&lt;h3 id=&quot;problem-statement-1&quot;&gt;Problem Statement&lt;/h3&gt;

&lt;p&gt;กำหนดกราฟระบุทิศทาง ระบุน้ำหนัก (directed, weighted graph)&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;มี node ทั้งหมด $n$ node&lt;/li&gt;
  &lt;li&gt;มี $m$ edge โดย edge ที่ $i$ เชื่อมจาก node $u_i$ ไป $v_i$ และมีค่าใช้จ่ายในการเดินทาง (น้ำหนัก) เท่ากับ $w_i$&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;หากเรามีสิทธิ์ในการเดินผ่าน edge โดยไม่เสียค่าใช้จ่ายได้ไม่เกิน $k$ เส้น จงหาว่าเราสามารถเดินทางจาก node $s$ ไปยัง node $t$ ได้โดยใช้ค่าใช้จ่ายน้อยสุดเท่าใด&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/state-graph-tutorial/figure4.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;กรณีตัวอย่างสำหรับโจทย์ข้อที่ 2&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;solution-idea-1&quot;&gt;Solution Idea&lt;/h3&gt;

&lt;p&gt;ตามที่โจทย์กำหนด ชัดเจนอยู่แล้วว่าจะต้องใช้อัลกอริทึมสำหรับหาเส้นทางสั้นสุดอย่าง Dijkstra’s Algorithm แน่ ๆ แต่เราไม่สามารถทำบนกราฟนี้ได้ได้ตรง ๆ เพราะเงื่อนไขพิเศษของโจทย์ข้อนี้ (สามารถเดินผ่านเส้นเชื่อมได้ฟรี ๆ $k$ ครั้ง)&lt;/p&gt;

&lt;p&gt;วิธีหนึ่งที่จะช่วยให้สามารถแก้โจทย์จำพวกนี้ได้ง่ายขึ้นคือการกำหนด&lt;strong&gt;สถานะการทำงาน/การเดินทาง/ฯลฯ (state)&lt;/strong&gt; ขึ้นมาให้สัมพันธ์กับโจทย์ และสังเกตว่าการเปลี่ยนสถานะทั้งหมดที่เป็นไปได้ มีอะไรบ้าง&lt;/p&gt;

&lt;p&gt;เช่น สำหรับข้อนี้ เราอาจจะกำหนดสถานะเป็นคู่อันดับ $(u, f)$ เพื่อแสดงให้เห็นว่า “ตอนนี้เราอยู่ node หมายเลขอะไร” และ “สามารถใช้สิทธิ์เดินผ่าน edge ฟรีได้อีกกี่ครั้ง”&lt;/p&gt;

&lt;p&gt;หากเราอยู่ที่ node $u$ สิ่งที่เราทำได้คือการเลือกว่าจะเดินไปยัง node ใดที่ติดอยู่กับ $u$ ต่อ (ตามกราฟที่โจทย์กำหนดให้มาใน input) สมมุติว่าเลือกไปยัง node $v$ โดยเส้นเชื่อมมีน้ำหนัก $w$ เราสามารถเลือกได้ว่า&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;จะเดินทางจาก node $u$ ไป node $v$ โดยเสียค่าใช้จ่ายตามปกติ ดังนั้น จะเป็นการเปลี่ยนสถานะจาก $(u, f) \to (v,f)$ โดยมีค่าใช้จ่ายเท่ากับ $w$ หรือ&lt;/li&gt;
  &lt;li&gt;จะเดินทางจาก node $u$ ไป node $v$ โดยใช้สิทธิ์เดินผ่านฟรี 1 ครั้ง ดังนั้น จะเป็นการเปลี่ยนสถานะจาก $(u,f) \to (v,f-1)$ โดยมีค่าใช้จ่ายเท่ากับ $0$ (วิธีนี้สามารถเลือกได้เมื่อ $f \geq 1$ เท่านั้น ถ้า $f=0$ คือหมดสิทธิ์แล้ว ห้ามใช้การเปลี่ยนสถานะนี้)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/state-graph-tutorial/figure5.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;ตัวอย่างการสร้าง State Graph จากกราฟเดิมที่กำหนดให้&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;เมื่อกำหนดสถานะได้เช่นนี้แล้ว การแก้โจทย์เดิมของเราสามารถทำได้โดย หาลำดับการเปลี่ยนสถานะที่เสียค่าใช้จ่ายน้อยที่สุด จากสถานะเริ่มต้น “อยู่ที่ node $s$ และเหลือสิทธิ์ $k$ สิทธิ์” ไปยังสถานะสิ้นสุด “อยู่ที่ node $t$ แต่เหลือสิทธิ์เท่าใดก็ได้ (สมมุติว่าเหลือ $i$ สิทธิ์)”&lt;/p&gt;

&lt;p&gt;หรือก็คือ การหาเส้นทางสั้นสุดบนกราฟสถานะ จากสถานะ $(s,k)$ ไปยังสถานะ $(t,?)$ ใด ๆ นั่นเอง&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/state-graph-tutorial/figure6.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;การหาเส้นทางสั้นสุดจาก $s = 0$ ไป $t = 3$ โดยอนุญาตให้ข้ามเส้นได้ $k = 1$ เส้น&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/state-graph-tutorial/figure7.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;การหาเส้นทางสั้นสุดจาก $s = 0$ ไป $t = 3$ โดยอนุญาตให้ข้ามเส้นได้ $k = 2$ เส้น&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;complexity-analysis-1&quot;&gt;Complexity Analysis&lt;/h3&gt;

&lt;p&gt;ถึงแม้ว่ากราฟดั้งเดิมของเราจะมี $n$ node $m$ edge ก็จริง แต่วิธีการแก้ปัญหาของเราจริง ๆ แล้วทำบนกราฟสถานะ (state graph) ที่เรากำหนดขึ้นมา ดังนั้นการวิเคราะห์ Time/Space Complexity จะต้องวิเคราะห์จากจำนวน node/edge บนกราฟสถานะดังกล่าว&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;วิธีของเรา สถานะขึ้นอยู่กับ node ที่อยู่และจำนวนสิทธิ์ที่เหลืออยู่ ดังนั้นจำนวนสถานะทั้งหมดคือ $\mathcal{O}(nk)$&lt;/li&gt;
  &lt;li&gt;จากแต่ละสถานะของเรา จะสามารถเปลี่ยนสถานะได้ตามจำนวน node ที่ติดอยู่กับ node ปัจจุบันคูณ 2 (เลือกใช้สิทธิ์/ไม่ใช้สิทธิ์เดินฟรี) ดังนั้น รวม ๆ แล้ว ถ้ากราฟมี $m$ node จำนวนเส้นเชื่อมสถานะจะอยู่ที่ประมาณ $\mathcal{O}(mk)$&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;หาก implement Dijkstra’s Algorithm โดยใช้ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;priority_queue&lt;/code&gt; ปกติแล้ว Time Complexity จะเป็น $\mathcal{O}(|E|+|V|\log|V|)$ ดังนั้น วิธีที่เราใช้แก้ข้อนี้จึงมี Time Complexity รวมเป็น $\mathcal{O}(mk + nk \log nk)$ นั่นเอง&lt;/p&gt;

&lt;p&gt;หากโจทย์ข้อนี้เป็นโจทย์การแข่งขัน ถ้า Time Limit เป็น 1 วินาที ก็จะมี constraint อยู่ที่ประมาณ $n \leq 10^5$, $m \leq 10^5$ และ $k \leq 10$&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href=&quot;https://ideone.com/3hipRU&quot;&gt;Solution Code 2&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;ข้อที่-3-hypergraph-traveling&quot;&gt;ข้อที่ 3: Hypergraph Traveling&lt;/h2&gt;

&lt;h3 id=&quot;problem-statement-2&quot;&gt;Problem Statement&lt;/h3&gt;

&lt;p&gt;กำหนดกราฟที่มี $n$ node $m$ &lt;strong&gt;hyperedge&lt;/strong&gt; มาให้ โดยที่ hyperedge คือเส้นเชื่อมที่ไม่ได้เชื่อมเพียงแค่ 2 node เท่านั้น &lt;strong&gt;อาจจะเชื่อมหลาย node ก็ได้&lt;/strong&gt; หากเราอยู่ที่ node ใด node หนึ่งของ hyperedge เราก็สามารถเลือกเดินทางไปยัง node อื่นที่เชื่อมด้วย hyperedge นี้ได้โดยเสียค่าใช้จ่ายเท่ากับน้ำหนักของ hyperedge นี้ จงหาเส้นทางสั้นสุดจาก node $s$ ไปยัง node $t$&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/state-graph-tutorial/figure8.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;กรณีตัวอย่างสำหรับโจทย์ข้อที่ 3&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;solution-idea-2&quot;&gt;Solution Idea&lt;/h3&gt;

&lt;h4 id=&quot;naive-approach&quot;&gt;Naive Approach&lt;/h4&gt;

&lt;p&gt;วิธีหนึ่งที่ง่ายที่สุดในการแก้โจทย์ข้อนี้ก็คือการแปลง hyperedge กลับเป็น edge ปกติ โดยสร้างเส้นเชื่อมระหว่างทุกคู่ node ใน hyperedge ดังภาพ&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/state-graph-tutorial/figure9.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;ตัวอย่างการสร้างกราฟตาม Naive Approach&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;ในกรณีที่มี hyperedge ที่เชื่อมระหว่างทุก node ในกราฟ วิธีการสร้างกราฟแบบนี้จะต้องสร้าง edge มากถึง $\mathcal{O}(n^2)$ ดังนั้น วิธีนี้ไม่เหมาะสมสำหรับการแก้โจทย์ข้อนี้&lt;/p&gt;

&lt;h4 id=&quot;better-approach&quot;&gt;Better Approach&lt;/h4&gt;

&lt;p&gt;อีกวิธีหนึ่งที่เราทำได้คือ สำหรับแต่ละ hyperedge ให้สร้าง node พิเศษขึ้นมา 1 node แล้วสร้าง edge เชื่อมจาก node ในกลุ่มมายัง node พิเศษ น้ำหนักเท่ากับค่าใช้จ่ายในการใช้ hyperedge แล้วสร้าง edge เชื่อมจาก node พิเศษนี้กลับไป node ในกลุ่มด้วยน้ำหนัก 0&lt;/p&gt;

&lt;p&gt;หากสร้างกราฟเช่นนี้ แต่ละ node จะไม่สามารถเดินทางหากันโดยตรงได้อีกต่อไป แต่จะต้องเดินทางผ่าน node พิเศษ เป็นตัวกลางในการเคลื่อนที่ของ hyperedge นั้น ๆ ดังภาพ&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/state-graph-tutorial/figure10.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;วิธีการสร้างกราฟโดยใช้ dummy node แทน hyperedge แต่ละกลุ่ม&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;เมื่อสร้างกราฟนี้ได้แล้วก็สามารถหาเส้นทางสั้นสุดได้ทันที&lt;/p&gt;

&lt;blockquote class=&quot;figure&quot;&gt;
  &lt;p&gt;&lt;img src=&quot;/post-extensions/state-graph-tutorial/figure11.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

  &lt;p&gt;การหาเส้นทางสั้นสุดโดยใช้กราฟที่สร้างขึ้น&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;complexity-analysis-2&quot;&gt;Complexity Analysis&lt;/h3&gt;

&lt;p&gt;เช่นเดียวกับข้อก่อนหน้า การวิเคราะห์ Time/Space Complexity จะต้องวิเคราะห์จากจำนวน node (จำนวนสถานะ) และจำนวน edge (การเปลี่ยนสถานะ) ของกราฟที่เราสร้างขึ้นมาใหม่ ดังนี้&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;กำหนดให้ hypergraph ที่โจทย์กำหนดให้มี $n$ node, $m$ hyperedge นอกจากนี้ กำหนดให้จำนวน node ที่ hyperedge แต่ละเส้นเชื่อมเข้าด้วยกัน รวมแล้วมีทั้งหมด $k$ node&lt;/li&gt;
  &lt;li&gt;กราฟใหม่ที่เราสร้างขึ้นมาจะมี $n+m$ node ($n$ node ปกติ และ $m$ node พิเศษสำหรับแต่ละ hyperedge)&lt;/li&gt;
  &lt;li&gt;เส้นเชื่อมที่สร้างจะมีทั้งหมด $2k$ เส้น (แต่ละ hyperedge มีเส้นเชื่อมไปยัง node ในกลุ่มสองทิศทาง)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;หากใช้ Dijkstra’s Algorithm ก็จะทำให้ Time Complexity เป็น $\mathcal{O}(k + n \log n)$ (สมมุติว่า $n \approx m$)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href=&quot;https://ideone.com/yug6xs&quot;&gt;Solution Code 3&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;บทสรุป&quot;&gt;บทสรุป&lt;/h2&gt;

&lt;p&gt;ตัวอย่าง 3 ข้อที่ยกมาอาจจะช่วยให้เห็นได้เพียงว่ามีเทคนิคแนวคิดแปลก ๆ ที่สามารถเอามาใช้กับการทำโจทย์ได้ แต่ก็อาจจะไม่ช่วยให้ทำโจทย์ได้มากขึ้นสักทีเดียว เพราะยังไง ๆ ก็ต้องใช้เวลาฝึกคิด/ฝึกโค้ดอยู่ดี ถ้าเป็นในไทยก็จะมีเว็บไซต์พวก &lt;a href=&quot;https://programming.in.th/&quot;&gt;programming.in.th&lt;/a&gt;, &lt;a href=&quot;https://www.codecube.in.th/&quot;&gt;CodeCube&lt;/a&gt;, &lt;a href=&quot;https://www.proprog.tk&quot;&gt;ProProg&lt;/a&gt; ที่มีโจทย์ดี ๆ เยอะแยะ ส่วนในต่างประเทศก็จะเป็นพวก &lt;a href=&quot;https://codeforces.com/&quot;&gt;Codeforces&lt;/a&gt;, &lt;a href=&quot;https://www.topcoder.com/community/competitive-programming&quot;&gt;TopCoder&lt;/a&gt;, &lt;a href=&quot;https://csacademy.com/&quot;&gt;CS Academy&lt;/a&gt; อะไรทำนองนี้&lt;/p&gt;

&lt;p&gt;หวังว่าบทความนี้จะช่วยคนที่สนใจเรื่องอัลกอริทึม (โดยเฉพาะสาย Competitive Programmer) ได้ไม่มากก็น้อยครับ :)&lt;/p&gt;

&lt;h3 id=&quot;recommended-problems&quot;&gt;Recommended Problems&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.codecube.in.th/task/185&quot;&gt;osu!mapping — CodeCube&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://programming.in.th/task/rev2_problem.php?pid=1133&quot;&gt;Subway — IOI Thailand League 2013&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.codecube.in.th/task/27&quot;&gt;หลอดไฟ (Bulb) — CodeCube&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.codecube.in.th/task/153&quot;&gt;Seven Gems — CodeCube&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.proprog.tk/tasks/toi14_logistic/descs/14202&quot;&gt;โลจิสติกส์ (Logistics) — TOI14&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.proprog.tk/tasks/toi15_fly/descs/15445&quot;&gt;ค่ายกลพิฆาตแมลงวัน (Fly) — TOI15&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sun, 18 Aug 2019 15:30:00 -0400</pubDate>
        <link>https://tcpc.me//2019/08/18/state-graph-tutorial.html</link>
        <guid isPermaLink="true">https://tcpc.me//2019/08/18/state-graph-tutorial.html</guid>
        
        <category>Tutorial</category>
        
        <category>Programming</category>
        
        <category>Algorithms and Data Structures</category>
        
        
      </item>
    
  </channel>
</rss>
