I will look at for and in-range and spend some more time exploring the built-ins. But I want to talk about the do-times solution, because that was the first thing I tried, but I couldn't make it work. In chapter 11 expression arguments are applied to something that builds up a result (as a list or an arithmetic operation). I don't understand how that applies to what I'm trying to do here since I'm not building anything, just calling it over and over and ignoring the result. Following what I learned from chapter 11 the code would look something like this, which doesn't make sense.<br>
<div><br><div><div>(define (do-times n expr)</div><div> (cond</div><div> [(< n 1) evaluate-expr-last-time]</div><div> [else ?? evaluate-expr (do-times (sub1 n) expr)]))</div><div><br></div><div><br></div><div><br>
</div><div><div class="gmail_quote">On Thu, Dec 9, 2010 at 13:08, Ryan Culpepper <span dir="ltr"><<a href="mailto:ryanc@ccs.neu.edu">ryanc@ccs.neu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On 12/09/2010 11:51 AM, Luke Jordan wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Here's a rookie question that stems from HtDP 29.3.2.<br>
<br>
The idea is to test an expression a number of times while timing it and<br>
compare to another version of the same function. The expression finds a<br>
route in a vector (graph) from node 0 to node 4. The way I would do<br>
this in C/Python is with a while loop, while the counter is not zero<br>
execute the expression (ignore return value), and wrap it in the timer.<br>
In Racket I came up with:<br>
<br>
(time (for-each<br>
(lambda (dest) (find-route 0 dest Graph))<br>
(make-list 1000 4)))<br>
<br>
It seems unnecessarily tricky, but I couldn't think of a better way. Am<br>
I missing a piece? Does it only seem tricky because Racket is my first<br>
functional language.<br>
</blockquote>
<br></div></div>
To the student of functional programming:<br>
<br>
You're right, it is unnecessary. You're using a list because you know someone has already provided "loop" functions for lists. But you just want to repeat an action N times---that's a natural number. Refresh yourself on Section 11 (Natural Numbers) and design this function:<br>
<br>
;; do-times : Nat (-> Void) -> Void<br>
;; Applies the given thunk N times.<br>
<br>
<br>
To the aspiring Racketeer:<br>
<br>
Racket has loop support for natural numbers, just not as a single function like 'for-each'. Look at 'for' and 'in-range'.<br><font color="#888888">
<br>
Ryan<br>
</font></blockquote></div><br></div></div></div>