[racket] How best to repeat a function call n times?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Sep 15 13:57:04 EDT 2014


Welcome to Racket v6.1.0.8.
> (define (repeater f count) (for ((i (in-range count))) (f)))
> (repeater (lambda () (displayln "hello world")) 10)
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world



On Sep 15, 2014, at 1:50 PM, George Rudolph <rudolphg1 at citadel.edu> wrote:

> All,
> I am sure this question has been answered before, but I can’t find it.
> Suppose I want to call some function, either built-in or user-defined, n number of times, and measure the total time it takes. Is there a more efficient or elegant way
> of doing this than passing the expression to a tail-recursive counting loop such as the following?
>  
> (define (repeater sexpr count)
>   (sexpr)
>   (cond
>     [(> count 0) (repeater sexpr (- count 1)) ])
> ) 
>  
>  
>  
> George Rudolph
> Associate Professor of Computer Science
> Department of Mathematics and Computer Science
> 225 Thompson Hall
> The Citadel
> 171 Moultrie Street
> Charleston, SC 29409
> 843.953.5032
>  
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



Posted on the users mailing list.