[erlang-questions] Programming a recursive function that executes n times

Raimo Niskanen raimo+erlang-questions@REDACTED
Fri Oct 22 09:30:52 CEST 2010


On Thu, Oct 21, 2010 at 11:11:01AM -0500, Fabio Miranda wrote:
> 
> Hello,
> 
> I want to simulate a time consuming task on Erlang, let's say I want to execute 10 000 times the above line:
> 
> test_mnesia:busqueda(cars).
> 
> 
> How can I accomplish this on Erlang?

Er, is this an absolute beginners question, or is it
an intricate expert question that I am missing the point in?...

Assuming it is an absolute beginners question this might worth the reading:
    http://www.erlang.org/starting.html

And here is an uncompiled program for you:
-module(foo).
-export([repeat/2]).
repeat(Fun, N) when is_function(Fun, 0), is_integer(N), N >= 0 ->
    if  N > 0 ->
	    Fun(),
	    repeat(Fun, N-1);
	true ->
	    ok
    end.

Call as:
    foo:repeat(fun () -> test_mnesia:busqueda(cars) end, 10000)

Another poster gave you an equivalent answer using a shell fun,
which is a wee bit more complicated but you it depends if
you want to do it from the shell or from a program.

If you want to collect the results another poster gave you a
suggestion a'la (allready in stdlib, just call):
    [test_mnesia:busqueda(cars) || X <- lists:seq(1, 10000)]
You get the results in a list, and can do it either from
the shell or from a program.

> 
> thanks,
> 
> fabio.
> 
> 
>  		 	   		  
-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list