how do i repeat a func in a loop, while returning the func just before each iteration

Ulf Wiger <>
Fri Aug 5 09:39:49 CEST 2005


Den 2005-08-05 02:22:58 skrev MEENA SELVAM <>:

> I would like to know how I can repeat the function
> nselect_cols say 10 times, each time the PId parameter
> being changed to 1,2, 3,... upto 10. But I should be
> able to get the return value as before.(return value
> of snasSessionDetailTable is what function foo
> returns)

There are some nice functions that operate on lists.
You can use some of those. Just to get you thinking,
I can suggest the following way to "loop" and collect
values:

Iters = lists:seq(1,10),  % -> [1,2,3,4,5,6,7,8,9,10]
[foo(...) || I <- Iters].

lists:foldl(
    fun(Iter, Acc) ->
        _NewAcc =
           case ... of
              ... -> ...;
              ...
           end
    end, InitialAcc, Iters).

I wouldn't worry too much about creating a list rather
than using a loop counter, but if your program is really
that performance-critical, you can write your own
iterator function in order to skip this step. It's a
good idea to adhere to the pattern of map and fold.

/Uffe
-- 
Ulf Wiger



More information about the erlang-questions mailing list