# Recursive anonymous functions

**URL:** <https://forum.elixirforum.com/t/recursive-anonymous-functions/18421>\
**Category:** Discussions\
**Tags:** recursion, anonymous-functions\
**Created:** [December 2, 2018, 8:21pm UTC](https://forum.elixirforum.com/t/recursive-anonymous-functions/18421 "2018-12-02T20:21:34Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![peerreynders](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/32/5826_2.png) [@peerreynders](https://forum.elixirforum.com/u/peerreynders)\
**Post date:** [December 3, 2018, 3:11pm UTC](https://forum.elixirforum.com/t/recursive-anonymous-functions/18421/7 "2018-12-03T15:11:53Z")

</div>

> [@jgonet](#):
>
> but this solution is a bit hacky.

```plaintext
1> G = fun                  
1> F (0, Acc) -> Acc;        
1> F (Num, Acc) -> F(Num - 1, Acc + 1)
1> end.
#Fun<erl_eval.36.128620087>
2> G(10,10).
20
3>

```

Even though this feature now exists in Erlang the points are:

1. There already is a solution in Elixir (and Erlang pre-17.0) that works even if it isn’t _elegant_.
2. How often does a _real need_ for recursive anonymous functions actually come up, given that recursion is well supported with named functions within actual code (rather than the shell)?

Erlang and by extension Elixir are _pragmatic_ languages.

> **[Beyond Functional Programming with Elixir and Erlang « Plataformatec Blog](https://blog.plataformatec.com.br/2016/05/beyond-functional-programming-with-elixir-and-erlang/)**
>
> José Valim reveals his point of view about the role of functional programming in Erlang and Elixir and what are its real benefits: maintainable, distributed, fault-tolerant applications.

I contend that anonymous functions are an _overused_ feature which are even more prevalent in Elixir because of the existence of the shorthand notation.

I see the primary value of an anonymous function in it’s closure (i.e. its connection to the scope that created it) - that should be the reason for using it. More often than not they are used simply for notational convenience even though quite often code becomes _less_ readable.

The [Erlang named anonymous function example](https://learnyousomeerlang.com/higher-order-functions#highlighter_833032) can be quite easily emulated with a mixture of an anonymous function for its closure and a named function to do the real work:

```Elixir
# file: Demo.ex
# Original: https://learnyousomeerlang.com/higher-order-functions#highlighter_833032
#
defmodule Demo do
  def prepare_alarm(room) do
    IO.puts("Alarm set in #{room}")
    fn -> loop(room) end
  end

  defp loop(room) do
    IO.puts("Alarm tripped in #{room}! Call Batman!")
    Process.sleep(500)
    loop(room)
  end
end

```

```plaintext
iex(1)> c("Demo.ex")
[Demo]
iex(2)> alarm_ready = Demo.prepare_alarm("bathroom")
Alarm set in bathroom
#Function<0.61175415/0 in Demo.prepare_alarm/1>
iex(3)> alarm_ready.()
Alarm tripped in bathroom! Call Batman!
Alarm tripped in bathroom! Call Batman!
Alarm tripped in bathroom! Call Batman!

BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
a

```

---

_[View the full topic](https://forum.elixirforum.com/t/recursive-anonymous-functions/18421)._
