lonesome-rider
Why function runs in loop when there is not a for loop
I’m new to Elixir and currently trying to learn the language by reading the Book “Programming Elixir ≥ 1.6”. I’m now trying to solve the exercise “ModulesAndFunctions-6”. I honestly had to read the solution because I wasn’t being able to solve it and what caught my attention was that the function used in the exercise runs like in a loop even though there’s no loop used here. I’m also pretty new to functional languages and I want to understand what’s going on here.
Hope you can help me clarify this.
Thanks
First Post!
Eiji
This is called a recursion. In short function calls itself as long as there is no call and therefore the result of expression at the end of function is returned. Elixir using a pattern-matching is doing a recursion based on input data. When it reach a specific function clause the algorithm finishes.
Let’s show it on simpler example:
defmodule Example do
# here we check if input is integer and then if it's less than 0
# if so we are using a recursive call with integer decreased by 1
def sample(integer) when is_integer(integer) and integer > 0, do: sample(integer - 1)
# here we check if input is integer and then if it's more than 0
# if so we are using a recursive call with integer increased by 1
def sample(integer) when is_integer(integer) and integer < 0, do: sample(integer + 1)
# here we have a literal check, it works as same as
# def sample(integer) when is_integer(integer) and integer == 0, do: :done
# so if we enter 0 as argument or the argument in recursion call is decreased/increased to 0
# then we return done atom
def sample(0), do: :done
end
iex> Example.sample(-5)
:done
iex> Example.sample(0)
:done
iex> Example.sample(5)
:done
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









