<html> <style type=“text/css”> .comment { font-style: italic; color: #c0342d; } .name, .variable, code var, pre var { color: #9e5cb1; } .name { font-weight: bold; } .variable, pre var, code var { font-style: italic; } .keyword { color: #abafb3; } .builtin, .string { color: #417fb8; } .constant { color: #e89f27; } code, code.pyret { color: black; } table { margin: 1em auto; } table th, table td { padding: 1pt 4pt; font-size: 13pt; } div.dw-content a { text-decoration: underline; } div.dw-content p, div.dw-content li, div.dw-content li p { font-size: 13pt; max-width: 750px; }

div.dw-content p+p, div.dw-content li+li, div.dw-content p+li, div.dw-content li + p { margin-top: 1em; }

div.dw-content ul li ul li { margin-top: 1em; } div.task { max-width: 750px; } p code, li code { font-size: 11pt; } blockquote { font-size: inherit; } pre kbd, code kbd { background: inherit; color: inherit; box-shadow: none; padding: 0; font: inherit; font-weight: bold; } a.secret { color: inherit; text-decoration: none !important; } a.secret:hover { text-decoration: underline !important; } ol li div.task p:first-child {

  margin: 0;

} div.task, p.task {

  background-color: #f2d4d7 !important;
  border: 1px solid #6366a !important;
  padding: 1em 2.25em;
  margin: 2em 1.5em;

} p.task, div.task p:first-child {

  text-indent: -1.5em;

}

blockquote {

  border: 0;
  margin: 1.5rem;

}

img { max-width: 100%; max-height: 800px; border: 1px solid black; } div.task img { max-width: 110%; }

figure {

   text-align: center;
   margin: 1rem auto 1rem auto;

}

figure img {

   padding: 0.5rem;
   border: 1px solid var(--border-color);

}

figure table {

   margin-left: auto;
   margin-right: auto;

}

table th {

   padding: 5px;
   font-style: italic;

}

table td {

   vertical-align: top;

}

table td, table th { text-align: center; }

table img {

   max-width: 75%;

} </style> <h1 id=“lab-7-call-the-plumber-or-a-tree-by-any-other-name”>Lab 7: Call the Plumber, <br> or, <a class=“secret” href=“https://en.wikipedia.org/wiki/A_rose_by_any_other_name_would_smell_as_sweet”><em>A Tree by Any Other Name</em></a></h1> <p>28 October 2022</p>

<h2 id=“learning-objectives”>Learning objectives</h2> <p>The purpose of this lab is to give you practice:</p> <ul> <li>modeling parts of the world using trees,</li> <li>working with structurally recursive data definitions, and</li> <li>writing functions using a template for structurally recursive data definitions.</li> </ul>

<hr style=“margin-top: 3em” /> <p>This lab can be completed in pairs!</p> <p>If you choose to work in a pair, you’ll make a single code file which you’ll upload to Gradescope with both your names.</p> <hr style=“margin-bottom: 3em” />

<h2 id=“introduction”>Introduction</h2>

<p>We would like to write a program that represents pipelines, as you might do if you were building a computer model of real-world infrastructure – or if you were making a game like <a href=“http://www.vgmpf.com/Wiki/index.php?title=File:Sim_City_2000_-_DOS_-_Water.png”>SimCity</a>!</p> <p>A pipeline has</p> <ul> <li>faucets, which can be opened or closed,</li> </ul> <table> <thead> <tr class=“header”> <th>faucet open</th> <th>faucet closed</th> </tr> </thead> <tbody> <tr class=“odd”> <td><img src=“https://www.cs.vassar.edu/~cs101/images/pipeline/faucet-on.png” alt=“image of an open faucet” /></td> <td><img src=“https://www.cs.vassar.edu/~cs101/images/pipeline/faucet-off.png” alt=“image of a closed faucet” /></td> </tr> </tbody> </table> <ul> <li>straight pipes, which can be copper or lead,</li> </ul> <table> <thead> <tr class=“header”> <th>one straight pipe leading to a faucet</th> <th>two straight pipes leading to a faucet</th> </tr> </thead> <tbody> <tr class=“odd”> <td><img src=“https://www.cs.vassar.edu/~cs101/images/pipeline/copper-faucet-off.png” /></td> <td><img src=“https://www.cs.vassar.edu/~cs101/images/pipeline/copper-lead-faucet-off.png” /></td> </tr> </tbody> </table> <ul> <li>and branches, where a pipe splits in two.</li> </ul> <table> <thead> <tr class=“header”> <th>pipeline with one branch</th> <th>pipeline with several branches</th> </tr> </thead> <tbody> <tr class=“odd”> <td><img src=“https://www.cs.vassar.edu/~cs101/images/pipeline/straight-branch-2-faucets.png” /></td> <td><img src=“https://www.cs.vassar.edu/~cs101/images/pipeline/pipeline.png” /></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.</p>

<p class=“task”><strong>Task</strong>: Copy the following data definitions and examples into your

lab07.arr

file.</p> <pre class=“pyret”>

data
Pipeline

:

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

:

| lead
| copper
end
#
# Example pipelines
#

<var>f-closed</var> = faucet(

false

) <var>f-open</var> = faucet(

true

)

<var>straight-c</var> = straight(copper, f-closed)

<var>straight-c-l</var> =

straight(copper,
  straight(lead, f-closed))

<var>branching-pipeline</var> =

branch(
  branch(
    straight(copper, 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

), it’s a good idea to write a template we can use for functions that take that data as input.</p> <p class=“task”><strong>Task</strong>: Copy this template into your

lab07.arr

file:</p> <pre class=“pyret”>

#|
fun pipeline-fun(pl :: Pipeline) -&gt; ...:
  doc: &quot;Template for a function that takes a pipeline&quot;
 
  cases (Pipeline) pl:
    | faucet(is-open) =&gt;
      ... is-open ...
    | straight(kind, pl1) =&gt;
      ... kind ...
      ... pipeline-fun(pl1) ...
    | branch(pl1, pl2) =&gt;
      ... pipeline-fun(pl1) ...
      ... pipeline-fun(pl2) ...
  end
where:
  pipeline-fun(...) is ...
end
|#
 
 
#
# Exercise 1
#
 
 
#
# Exercise 2
#
 
 
#
# Exercise 3
#
 
 
#
# Exercise 4
#
 
 
#
# Exercise 5
#

</pre>

<h2 id=“exercise-1”>Exercise 1</h2>

<p class=“task”><strong>Task</strong>: Implement the function

is-water-running

, which takes a

Pipeline

and determines whether any faucets are open.</p>

<p>To help you get started with the lab, we’ve done this one for you!</p>

<pre class=“pyret”>

#
# Exercise 1
#
fun
is-water-running

(pl :: Pipeline) -&gt; Boolean:

<code class="keyword">doc</code>: <code class="string">&quot;Determine whether any faucets are open in pl&quot;</code>
<code class="keyword">cases</code> (Pipeline) pl:
  | faucet(is-open) =&gt;
    is-open
  | straight(kind, pl1) =&gt;
    is-water-running(pl1)
  | branch(pl1, pl2) =&gt;
    is-water-running(pl1) <code class="keyword">or</code> is-water-running(pl2)
<code class="keyword">end</code>
where

:

is-water-running(f-open) <code class="keyword">is</code> <code class="constant">true</code>
is-water-running(f-closed) <code class="keyword">is</code> <code class="constant">false</code>
is-water-running(straight-c) <code class="keyword">is</code> <code class="constant">false</code>
is-water-running(straight-c-l) <code class="keyword">is</code> <code class="constant">false</code>
is-water-running(branching-pipeline) <code class="keyword">is</code> <code class="constant">true</code>
end

</pre>

<p>Copy and paste it into your

lab07.arr

file. Make sure you understand how this function works – how did we write this based on the template above?</p>

<p class=“task”><strong>Task</strong>: Why do we have so many examples for this function? Since it’s a predicate (a function returning a Boolean), why isn’t it enough to have one

true

example and one

false

example? Put your answer in a comment.</p>

<h2 id=“exercise-2”>Exercise 2</h2>

<p class=“task”><strong>Task</strong>: Implement the function

count-faucets

, which takes a

Pipeline

and counts the number of faucets it contains.</p>

<p>To do this,</p> <ul> <li>Copy and paste the template.</li> <li>Fill in <ul> <li>the desired function name (everywhere it occurs),</li> <li>the docstring, and then</li> <li>the examples.</li> </ul></li> <li>Using the examples, replace the placeholders (

...

) in the template with appropriate code.</li> </ul>

<h2 id=“exercise-3”>Exercise 3</h2>

<p class=“task”><strong>Task</strong>: Implement the function

count-open

, which takes a

Pipeline

and counts the number of <em>open</em> (that is, running) faucets.</p> <p>Follow the same steps!</p>

<h2 id=“exercise-4”>Exercise 4</h2>

<p class=“task”><strong>Task</strong>: Implement the function

modernize

, which takes a

Pipeline

and converts all the (

straight

)

lead

pipes to

copper

ones.</p> <p><strong>Hint</strong>: You don’t need an

if

to do this!</p>

<h2 id=“exercise-5”>Exercise 5</h2> <p class=“task”><strong>Task</strong>: Implement the function

off

, which takes a

Pipeline

and turns off all the faucets.</p> <h2 id=“submitting-the-lab”>Submitting the lab</h2> <ul> <li><p>When you’ve completed the exercises, show your code to your instructor or one of the coaches.</p></li> <li><p>Then upload your

lab07.arr

file to the Lab 7 assignment on <a href=“https://www.gradescope.com”>Gradescope</a>.</p></li> </ul>

<h2 id=“acknowledgments”>Acknowledgments</h2> <p>This lab includes material adapted from Marc Smith, Vassar College.</p> </html>