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

myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement