Skip to content

Commit 687d3fe

Browse files
spadarjauhienegonSchiele
authored andcommitted
Add Elixir examples for recursion
1 parent fa35ae8 commit 687d3fe

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

03_recursion/elixir/01_countdown.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defmodule Countdown do
2+
defguardp non_positive?(x) when x <= 0
3+
4+
def from(i) when non_positive?(i), do: nil
5+
6+
def from(i) do
7+
IO.puts(i)
8+
from(i - 1)
9+
end
10+
end
11+
12+
Countdown.from(5)

03_recursion/elixir/02_greet.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule Greeting do
2+
def greet2(name) do
3+
IO.puts("how are you, #{name}?")
4+
end
5+
6+
def bye do
7+
IO.puts("ok bye!")
8+
end
9+
10+
def greet(name) do
11+
IO.puts("hello, #{name}!")
12+
greet2(name)
13+
IO.puts("getting ready to say bye...")
14+
bye()
15+
end
16+
end
17+
18+
Greeting.greet("adit")

03_recursion/elixir/03_factorial.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
defmodule Factorial do
2+
def of(1), do: 1
3+
def of(n), do: n * of(n - 1)
4+
end
5+
6+
IO.puts(Factorial.of(5))

0 commit comments

Comments
 (0)