# [racket] Rookie Question on Functional Languages

 From: Ryan Culpepper (ryanc at ccs.neu.edu) Date: Thu Dec 9 14:08:12 EST 2010 Previous message: [racket] Rookie Question on Functional Languages Next message: [racket] Rookie Question on Functional Languages Messages sorted by: [date] [thread] [subject] [author]

```On 12/09/2010 11:51 AM, Luke Jordan wrote:
>   Here's a rookie question that stems from HtDP 29.3.2.
>
> The idea is to test an expression a number of times while timing it and
> compare to another version of the same function.  The expression finds a
> route in a vector (graph) from node 0 to node 4.  The way I would do
> this in C/Python is with a while loop, while the counter is not zero
> execute the expression (ignore return value), and wrap it in the timer.
>   In Racket I came up with:
>
> (time (for-each
>         (lambda (dest) (find-route 0 dest Graph))
>         (make-list 1000 4)))
>
> It seems unnecessarily tricky, but I couldn't think of a better way.  Am
> I missing a piece?  Does it only seem tricky because Racket is my first
> functional language.

To the student of functional programming:

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:

;; do-times : Nat (-> Void) -> Void
;; Applies the given thunk N times.

To the aspiring Racketeer:

Racket has loop support for natural numbers, just not as a single
function like 'for-each'. Look at 'for' and 'in-range'.

Ryan

```

 Posted on the users mailing list. Previous message: [racket] Rookie Question on Functional Languages Next message: [racket] Rookie Question on Functional Languages Messages sorted by: [date] [thread] [subject] [author]