Iterate n times

Francesco Cesarini <>
Tue Jan 22 20:53:40 CET 2002


Another function I have often implemented and used in my private 
libraries is

while(Fun, Arg) -> while(Fun(Arg), Fun).

while({true, Arg},   Fun) -> while(Fun(Arg), Fun);
while({false, Val}, _Fun) -> Val.

Francesco
--
http://www.erlang-consulting.com

Sean Hinde wrote:

>Does anyone know if this function has a proper name or already exists
>anywhere?
>
>iter(F, St, 0) ->
>    St;
>iter(F, St, N) when integer(N), N > 1 ->
>    iter(F, F(St, N), N-1).
>
>Simple examples of usage might be:
>
>1> iter(fun(St, N) -> N + St end, 0, 5).
>15
>2> iter(fun(St, N) -> [N|St] end, [], 5).
>[1,2,3,4,5]
>
>Sean
>
>
>
>NOTICE AND DISCLAIMER:
>This email (including attachments) is confidential.  If you have received
>this email in error please notify the sender immediately and delete this
>email from your system without copying or disseminating it or placing any
>reliance upon its contents.  We cannot accept liability for any breaches of
>confidence arising through use of email.  Any opinions expressed in this
>email (including attachments) are those of the author and do not necessarily
>reflect our opinions.  We will not accept responsibility for any commitments
>made by our employees outside the scope of our business.  We do not warrant
>the accuracy or completeness of such information.
>
>
>
>





More information about the erlang-questions mailing list