[erlang-questions] beginner question

Håkan Stenholm hokan.stenholm@REDACTED
Sun Aug 17 05:37:11 CEST 2008


Ron Peterson wrote:

> When I compile the code below and run it, I get something like the
> following:
>
> 54> scrap:random_tuples( 20, 5 ).
> [{3,9},{3,9},{3,9},{3,9},{3,9}]
>
> I'm sure this is expected behaviour.  I'm just not yet familiar enough
> with erlang to understand it.  Why isn't this the same as calling
> random_tuple a number of times in succession?
>
> TIA.
>
> set_seed_to_now() ->
>     {A1, A2, A3} = now(),
>     random:seed( A1, A2, A3 ).
>
> for( Max, Max, F ) ->
>     [F];
> for( I, Max, F ) ->
>     [F|for( I+1, Max, F )].
>
> random_tuple( Max ) ->
>     { random:uniform( Max ), random:uniform( Max ) }.
>
> random_tuples( Max, Num ) ->
>     set_seed_to_now(),
>     for( 1, Num, random_tuple( Max ) ).
you probably want to do something like:

for( 1, Num, fun() -> random_tuple( Max ) end).

and call F in for(...) as F() so that you pass a function to for(...)
to run in each iteration, rather than simply passing a precalculated
value as you do right now.



More information about the erlang-questions mailing list