Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are three styles of loops available in Racket: </p> <h3>#1 The for-loop style</h3> <p>This style uses the syntax described in Chapter 11 of the Racket Guide, <a href="http://docs.racket-lang.org/guide/for.html" rel="nofollow">"Iterations and Comprehensions"</a>. It looks like this:</p> <pre><code>(require slideshow/pict) (for/fold ([result (blank)]) ([i (in-range 9)]) (hc-append (circle 10) result)) </code></pre> <p>In this style, in the first parenthesis after <em>for</em> defines the loop's temporary variables. There is one such variable in my example, called <em>result</em>. Then you define the iteration variables and what they iterate over. So here, <em>i</em> loops over the numbers for 0 to 8. The body of the loop runs once for each <em>i</em>, each time the result is assigned to <em>result</em>, and the final value of the loop is the value of <em>result</em> at the end.</p> <h3>#2 The map and fold style.</h3> <p>This style is described in <a href="http://docs.racket-lang.org/guide/pairs.html" rel="nofollow">Section 3.8 of the Guide</a>. It looks like this:</p> <pre><code>(require slideshow/pict) (foldl hc-append (blank) (build-list 9 (lambda (i) (circle 10)))) </code></pre> <p>This code start by making a list of 9 circles:</p> <pre><code>(define o (circle 10)) (build-list 9 (lambda (i) o) ---&gt; (list o o o o o o o o o) </code></pre> <p>The <em>list</em> notation is a shorthand for the more verbose <em>cons</em> notation.</p> <pre><code>(list o o o o o o o o o) ---&gt; (cons o (cons o (cons o (cons o (cons o (cons o (cons o (cons o (cons o empty))))))))) </code></pre> <p>The way to think of the <em>foldl</em> function is that it turns a piece of data into a piece of computation. It takes a list as an input and it turns it into a collection of function calls.</p> <p>The <em>foldl</em> function takes the list and replaces each <em>cons</em> in the list with the first argument, and replaces the <em>empty</em> at the end of the list with the second argument. In the example, I pass the <em>hc-append</em> function and (blank), so the replacement looks like this:</p> <pre><code>(foldl hc-append (blank) ...) ---&gt; (hc-append o (hc-append o (hc-append o (hc-append o (hc-append o (hc-append o (hc-append o (hc-append o (hc-append o (blank)))))))))) </code></pre> <p>This sequence of function calls is exactly the one you wanted to avoid writing by hand. <em>Foldl</em> computes it for you.</p> <h3>#3 The recursion style</h3> <p>This is the style Roddy described in his answer. As a general matter of coding style, you should only use the recursion style if there is no simple *for, <em>map</em>, or <em>fold</em> possible, such as when traversing a recursive data structure.</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. 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