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

Last Post!

cjbottaro

cjbottaro

Recursion: when a function calls itself.

defmodule Infinite do
  def loop do
    IO.puts "stuck in an infinite loop"
    loop()
  end
end

iex> Infinite.loop()

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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 55125 245
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New

We're in Beta

About us Mission Statement