[racket] Rookie Question on Functional Languages

From: Joe Marshall (jmarshall at alum.mit.edu)
Date: Thu Dec 9 15:07:08 EST 2010

On Thu, Dec 9, 2010 at 11:48 AM, Luke Jordan <luke.jordan at gmail.com> wrote:
>  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.

Right.  Timing something is definitely `non-functional' (there is an obvious
implicit dependence upon time).

> Following what I learned from
> chapter 11 the code would look something like this, which doesn't make
> sense.
> (define (do-times n expr)
>   (cond
>     [(< n 1) evaluate-expr-last-time]
>     [else ?? evaluate-expr (do-times (sub1 n) expr)]))

This is very close.  There are two things you want to change.

First, you really can't hand in an expression.  You'll want to hand
in a procedure that returns the value of the expression (after
presumably calculating it!).  This procedure will have no arguments.
When you need the value, simply invoke the procedure.

Second, because of the implicit time dependency, you'll want to
explicitly run the procedure before the recursive call to do-times.
You just about have it correct in that last line.  Try this:
   (else  (begin  (procedure)
                       (do-times (sub1 n) procedure)))

(For extra credit, be sure that (do-times 0 ...) does not call
the procedure at all.)


Posted on the users mailing list.