<div dir='rtl'><p style="text-align: left;" dir="ltr">Hi,</p>
<p style="text-align: left;" dir="ltr"> </p>
<p style="text-align: left;" dir="ltr">If the goal is to traverse the list only once, it can be done with fold.</p>
<p style="text-align: left;" dir="ltr"> </p>
<p style="text-align: left;" dir="ltr"> </p>
<p style="text-align: left;" dir="ltr"> </p>
<p style="text-align: left;" dir="ltr">A = [1,2,3,4,5,1,3,2,4,3,5,7,7,7,1,2,3,4,5].</p>
<p style="text-align: left;" dir="ltr"> </p>
<p style="text-align: left;" dir="ltr">F = fun (N, {P,A1,A2}) -></p>
<p style="text-align: left;" dir="ltr">{P1,P2} = P(N),</p>
<p style="text-align: left;" dir="ltr">{P, A1 + P1, A2 + P2} end.</p>
<p style="text-align: left;" dir="ltr"> </p>
<p style="text-align: left;" dir="ltr">lists:foldl(F, {fun(N) -> case (N > 3) of true -> {1,0}; _ -> {0,1} end end ,0,0}, A).</p>
<p style="text-align: left;" dir="ltr"> </p>
<p style="text-align: left;" dir="ltr"> </p>
<p style="text-align: left;" dir="ltr"> </p>
<p style="text-align: left;" dir="ltr">Sorry for the top post and so badly formatted message,</p>
<p style="text-align: left;" dir="ltr">Atal</p>
<p> </p>
<p style="text-align: left;"> </p>
<p style="text-align: left;"> </p>
<div class="walSignature" style="text-align: left;"><span style="font-size: 10pt;">שולח: <strong>Attila Rajmund Nohl</strong><br />נושא: <strong>Re: [erlang-questions] To name a function</strong></span></div>
<pre style="text-align: left;">2010/12/15, Ryan Zezeski :
> On Wed, Dec 15, 2010 at 11:06 AM, Attila Rajmund Nohl <
> attila.r.nohl@gmail.com> wrote:
>
>> Hello!
>>
>> I've found at least places in my code where I'd need a function which
>> decides if all, some or none members of a list have a particular
>> property (the list always has at least one element). How to name this
>> function? The code is something like this:
>>
>> f(_F, []) ->
>> erlang:error({badarg, []});
>>
>> f(F, L) ->
>> {Has, Not} =
>> lists:foldr(
>> fun(E, {H, N}) ->
>> case f(E) of
>> true -> {H+1, N};
>> false -> {H, N+1}
>> end
>> end, {0,0}, L),
>> case {Has, Not} of
>> {0, _} -> none;
>> {_, 0} -> all;
>> _ -> some
>> end.
>>
>>
> Well, I wouldn't give it any style points but I came up with all_some_none.
> Also, instead of using fold you could make use of lists:all/2 and
> lists:filter/2.
I could, but that would traverse the list twice (with the length()
guard three times). I don't expect the lists I'm using this function
on to have more than 2000 elements, so on contemporary hardware this
difference is probably not noticeable.
________________________________________________________________
erlang-questions (at) erlang.org mailing list.
<a href="http://www.erlang.org/faq.html">http://www.erlang.org/faq.html</a>
<a href="mailto:erlang-questions-unsubscribe@erlang.org">mailto:erlang-questions-unsubscribe@erlang.org</a>
</pre></div><hr><div style="background-color:white;color:black;">Walla! Mail - <a href="http://www.walla.co.il" style="color:blue">Get your free unlimited mail today</a></div>