Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first thing you need to realize is that "looping" in Racket is really just recursion. In this case, you want to chain a bunch of drawing calls together. If we wrote this out, our target goal would be this:</p> <pre><code>(hc-append (circle 10) (hc-append (circle 10) (hc-append (circle 10) (hc-append (circle 10) (hc-append (circle 10) (hc-append (circle 10) (hc-append (circle 10) (hc-append (circle 10) (hc-append (circle 10)))))))))) </code></pre> <p>I am assuming all of our circles are going to be the same radius.</p> <p>Now, since we're going to be writing a recursive method, we need to think of our base case. We want exactly nine circles drawn. Let's call this maximum number of circles <code>max</code>. Our base case, when we break out of our "loop" will be when we reach <code>max</code> iterations, or when <code>(= iterations max)</code>.</p> <p>Now for the recursion itself. We already know we need to pass in at least two variables, the current iteration <code>iterations</code>, and the maximum iteration <code>max</code>. If you look at the code above, you'll notice that the repeating element in all of the "loops" is the <code>(circle 10)</code>. Now there are a number of ways you could pass that along -- some people would choose to just pass the radius for example -- but I think the easiest way would be to pass in the pict of the circle. </p> <p>Finally, we also have to pass along the picture we've done so far. That is, as we append a circle to our chain, we need to pass this back into the recursive method so we can keep appending.</p> <p>Now that we've got that squared away, we can define the structure of our recursive method, which we shall call <code>circle-chain-recursive</code>:</p> <pre><code>(define (circle-chain-recursive iteration max crcle output) ; body here ) </code></pre> <p>The "guts" of our method will be an <code>if</code>. If we've reached the max iteration, return the output. Otherwise append another circle, increment <code>iteration</code>, and call the method again.</p> <pre><code>(define (circle-chain-recursive iteration max crcle output) (if (= iteration max) output (circle-chain-recursive (+ 1 iteration) max crcle (hc-append crcle output)))) </code></pre> <p>I personally don't like calling recursive looping methods like this directly, so I would write a helper method like this:</p> <pre><code>(define (circle-chain num radius) (circle-chain-recursive 0 num (circle radius) (circle 0))) </code></pre> <p>Now if I want a series of 9 circles with radius 10, all I have to do is call <code>(circle-chain 9 10)</code>.</p> <p>You'll notice that I passed <code>(circle 0)</code> as in as the parameter called <code>output</code>. This is because the <code>hc-append</code> method requires a <code>pict</code> parameter. Since we're not starting out with any circles, I passed it the equivalent of a "blank" or nil pict. There may be some other way of passing a "blank" pict, but I'm not too familiar with the <code>slideshow/pict</code> libraries to know it.</p> <p>I hope that clears it up a bit.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload