[erlang-questions] beginner question

Richard A. O'Keefe <>
Mon Aug 18 02:50:24 CEST 2008


On 17 Aug 2008, at 3:07 pm, Ron Peterson wrote:
> for( Max, Max, F ) ->
>    [F];
> for( I, Max, F ) ->
>    [F|for( I+1, Max, F )].

There are two issues here.
First, for(1, 0, F) will crash instead of returning an empty list.
Why?

Second, you are making a list all of whose elements are the
*value* F.  With a name like that, you almost certainly wanted

for(LB, UB, Fun) ->
     [Fun() || I <- lists:seq(LB, UB)].

or even

for(LB, UB, Fun) ->
     [Fun(I) || I <- lists:seq(LB, UB)].

Better still, ditch for/3 entirely, and write
the list comprehension in place.
>
>
> random_tuple( Max ) ->
>    { random:uniform( Max ), random:uniform( Max ) }.
>
> random_tuples( Max, Num ) ->
>    set_seed_to_now(),
>    for( 1, Num, random_tuple( Max ) ).

This should be

random_tuples(Max, Count) ->
     set_seed_to_now(),
     [random_tuple(Max) || I <- lists:seq(1, Count)].

--
If stupidity were a crime, who'd 'scape hanging?










More information about the erlang-questions mailing list