<html> <h1 id=“lab-7-call-the-plumber-_a-k-a-_-a-tree-by-any-other-name-”>Lab 7: Call the Plumber (<em>a.k.a.</em> A Tree By Any Other Name)</h1> <h2 id=“learning-objectives”>Learning objectives</h2> <p>The purpose of this lab is to:</p> <ul> <li>give you practice modeling phenomena in our world using Trees</li> <li>give you practice working with structurally recursive data definitions </li> <li>give you practice writing functions using a template for structurally recursive data definitions</li> </ul> <h2 id=“setup”>Setup</h2> <p>Create the file

lab07.arr

and copy/paste the following header comment into it:</p> <pre>

<span class="hljs-comment">#|</span>
  CMPU<span class="hljs-number">-101</span>
  Fall <span class="hljs-number">2021</span>
  Lab <span class="hljs-number">7</span>
  Name: your <span class="hljs-built_in">name</span>
|<span class="hljs-comment">#</span>

</pre><p>As usual, ask your instructor or a coach if you&#39;re stuck. Remember to save your work every so often.</p> <h2 id=“description”>Description</h2> <p>We would like to represent pipelines in our program. A pipeline has faucets (opened or closed):</p> <table> <thead> <tr> <th>faucet open</th> <th>&nbsp; &nbsp; &nbsp; faucet closed</th> </tr> </thead> <tbody> <tr> <td><img src=“https://www.cs.vassar.edu/~cs101/images/pipeline/faucett-on.png” alt=“image of an open faucet”></td> <td>&nbsp; &nbsp; &nbsp; <img src=“https://www.cs.vassar.edu/~cs101/images/pipeline/faucett-off.png” alt=“image of an open faucet”></td> </tr> </tbody> </table> <p>straight parts (copper or lead): </p> <table> <thead> <tr> <th>one straight pipe leading to a faucet</th> <th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; two straight pipes leading to a faucet</th> </tr> </thead> <tbody> <tr> <td><img src=“https://www.cs.vassar.edu/~cs101/images/pipeline/copper-faucet-off.png” alt=“image of an open faucet”></td> <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src=“https://www.cs.vassar.edu/~cs101/images/pipeline/copper-lead-faucet-off.png” alt=“image of an open faucet”></td> </tr> </tbody> </table> <p>and branches. Here are two pipelines with branches: </p> <table> <thead> <tr> <th>pipeline with one branch</th> <th>pipeline with several branches</th> </tr> </thead> <tbody> <tr> <td><img src=“https://www.cs.vassar.edu/~cs101/images/pipeline/straight-branch-2-faucets.png” alt=“image of an open faucet”></td> <td><img src=“https://www.cs.vassar.edu/~cs101/images/pipeline/pipeline.png” alt=“image of an open faucet”></td> </tr> </tbody> </table> <h2 id=“data-definition”>Data Definition</h2> <p>We can represent pipelines as described above with the appropriate data definition. Since our pipelines consist of multiple instances of faucets, straight pipes, and branches, our data definition includes these three varieties. Copy this data definition and examples of pipelines into your &#39;&#39;lab07.arr&#39;&#39; file:</p> <pre>

data Pipeline:
  | faucet(open :: Boolean)
  | straight(kind :: String, pl :: Pipeline)
  | branch(pl1 :: Pipeline, pl2 :: Pipeline)
end

# Examples of pipelines
f-closed = faucet(<span class="hljs-keyword">false</span>)
f-open = faucet(<span class="hljs-keyword">true</span>)
straight-cop = straight(<span class="hljs-string">"copper"</span>, f-closed)
straight-cop-lead = 
  straight(<span class="hljs-string">"copper"</span>, 
    straight(<span class="hljs-string">"lead"</span>, f-closed))
branching-pipeline = 
  branch(
    branch(
      straight(<span class="hljs-string">"copper"</span>, f-open), 
      f-closed),
    branch(
      f-closed,
      f-closed))

</pre><h2 id=“template”>Template</h2> <p>Since we defined a new type of data (Pipeline), we need to create a new Template for functions that take a Pipeline as input. Copy this template into your &#39;&#39;lab07.arr&#39;&#39; file:</p> <pre>

<span class="hljs-comment">#|</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">pipeline</span></span>-<span class="hljs-function"><span class="hljs-keyword">fun</span>(<span class="hljs-title">pl</span></span> :: Pipeline) -&gt; ...:
  <span class="hljs-symbol">doc:</span> <span class="hljs-string">"template for a function that takes a pipeline"</span>
  cases (Pipeline) <span class="hljs-symbol">pl:</span>
    | faucet(open) =&gt; 
      ... open ...
    | straight(kind, pl1) =&gt; 
      ... kind ... 
      ... pipeline-<span class="hljs-function"><span class="hljs-keyword">fun</span>(<span class="hljs-title">pl1</span></span>) ...
    | branch(pl1, pl2) =&gt;
      ... pipeline-<span class="hljs-function"><span class="hljs-keyword">fun</span>(<span class="hljs-title">pl1</span></span>) ...
      ... pipeline-<span class="hljs-function"><span class="hljs-keyword">fun</span>(<span class="hljs-title">pl2</span></span>) ...
  <span class="hljs-keyword">end</span>
<span class="hljs-symbol">where:</span> 
  pipeline-<span class="hljs-function"><span class="hljs-keyword">fun</span>(...) <span class="hljs-title">is</span></span> ...
<span class="hljs-keyword">end</span>
|<span class="hljs-comment">#</span>

<span class="hljs-comment"># Exercise 1</span>

<span class="hljs-comment"># Exercise 2</span>

<span class="hljs-comment"># Exercise 3</span>

<span class="hljs-comment"># Exercise 4</span>

<span class="hljs-comment"># Exercise 5</span>

</pre><h2 id=“exercise-1”>Exercise 1</h2> <p>Implement the function, &#39;&#39;is-water-running&#39;&#39;, which takes a pipeline and determines whether any faucets are open.</p> <p>To help you get started, here&#39;s our solution. Copy/paste it into your &#39;&#39;lab07.rkt&#39;&#39; file. Study it, run it, and make sure you understand how it works. Then go on to the next exercise.</p> <pre>

# Exercise <span class="hljs-number">1</span>

<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-keyword">is</span>-water-<span class="hljs-title">running</span><span class="hljs-params">(pl :: <span class="hljs-type">Pipeline</span>)</span></span> -&gt; <span class="hljs-built_in">Boolean</span>:
  doc: <span class="hljs-string">"determines whether any faucets are open in pl"</span>
  cases (Pipeline) pl:
    | faucet(<span class="hljs-keyword">open</span>) =&gt; 
      <span class="hljs-keyword">open</span>
    | straight(kind, pl1) =&gt; 
      <span class="hljs-keyword">is</span>-water-running(pl1)
    | branch(pl1, pl2) =&gt;
      <span class="hljs-keyword">is</span>-water-running(pl1) or <span class="hljs-keyword">is</span>-water-running(pl2)
  end
where: 
  <span class="hljs-keyword">is</span>-water-running(f-<span class="hljs-keyword">open</span>) <span class="hljs-keyword">is</span> <span class="hljs-literal">true</span>
  <span class="hljs-keyword">is</span>-water-running(f-closed) <span class="hljs-keyword">is</span> <span class="hljs-literal">false</span>
  <span class="hljs-keyword">is</span>-water-running(straight-cop) <span class="hljs-keyword">is</span> <span class="hljs-literal">false</span>
  <span class="hljs-keyword">is</span>-water-running(straight-cop-lead) <span class="hljs-keyword">is</span> <span class="hljs-literal">false</span>
  <span class="hljs-keyword">is</span>-water-running(branching-pipeline) <span class="hljs-keyword">is</span> <span class="hljs-literal">true</span>
end

</pre><h2 id=“exercise-2”>Exercise 2</h2> <p>Implement the function, &#39;&#39;count-faucets&#39;&#39;, which takes a pipeline and counts the number of faucets.</p> <h2 id=“exercise-3”>Exercise 3</h2> <p>Implement the function, &#39;&#39;count-open&#39;&#39;, which takes a pipeline and counts the number of open faucets.</p> <h2 id=“exercise-4”>Exercise 4</h2> <p>Implement the function, &#39;&#39;modernize&#39;&#39;, which takes a pipeline and converts all &quot;lead&quot; straight pipes to &quot;copper&quot;</p> <h2 id=“exercise-5”>Exercise 5</h2> <p>Implement the function, &#39;&#39;off&#39;&#39;, which takes a pipeline and turns off all the faucets</p> </html>