Anonymous functions or lambdas are a basic building box in any functional programmer's toolbox. Examples of functions that take other anonymous functions in the standard library are lists:map, lists:foldl, lists:filter, and their analogs in the orddict library, sets library, etc. Of course, any of those parameters can be named functions as well but if the function is simple enough, an anonymous function is preferred for brevity (example, lists:map(fun(Elem) -> Elem * 2 end, List) doubles the value of all elements in List).<div>
<br></div><div>You can also accept a function as a parameter for any of your functions too. For example:</div><div><br></div><div>every_other(Fun, List) -></div><div>  {_, Res} = lists:foldl(fun(Elem, {Idx, Acc}) -> case Idx rem 2 of 0 -> {Idx + 1, Acc ++ [Elem]}; 1 -> {Idx + 1, Acc ++ [Fun(Elem)]} end, List),</div>
<div>  Res.</div><div><br></div><div>This returns a list with Fun applied to every other element.</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Nov 26, 2012 at 12:09 AM, Lucky Khoza <span dir="ltr"><<a href="mailto:mrkhoza@gmail.com" target="_blank">mrkhoza@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Morning Erlang Developers,<br><br>I would like to ask about: when do we use funs or anonymous functions in Erlang?, I know what it is, but I can't really figure out when to use it and I saw an example of it being used with Mnesia transactions. <br>

<br>May someone help in this regard please.<br><br>Kindest Regards<span class="HOEnZb"><font color="#888888"><br>Lucky Khoza<br>
</font></span><br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>