<p>We make a function <span class="fixed">askForNumber</span>, which takes a random number generator and returns an I/O action that will prompt the user for a number and tell him if he guessed it right. In that function, we first generate a random number and a new generator based on the generator that we got as a parameter and call them <span class="fixed">randNumber</span> and <span class="fixed">newGen</span>. Let’s say that the number generated was <span class="fixed">7</span>. Then we tell the user to guess which number we’re thinking of. We perform <span class="fixed">getLine</span> and bind its result to <span class="fixed">numberString</span>. When the user enters <span class="fixed">7</span>, <span class="fixed">numberString</span> becomes <span class="fixed">"7"</span>. Next, we use <span class="fixed">when</span> to check if the string the user entered is an empty string. If it is, an empty I/O action of <span class="fixed">return ()</span> is performed, which effectively ends the program. If it isn’t, the action consisting of that <i>do</i> block right there gets performed. We use <span class="fixed">read</span> on <span class="fixed">numberString</span> to convert it to a number, so <span class="fixed">number</span> is now <span class="fixed">7</span>.</p>
0 commit comments