lonesome-rider

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

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

Where Next?

Popular in Questions Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New

We're in Beta

About us Mission Statement