Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
14
Task/Factorial/Elixir/factorial.elixir
Normal file
14
Task/Factorial/Elixir/factorial.elixir
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
defmodule Factorial do
|
||||
# Simple recursive function
|
||||
def fac(0), do: 1
|
||||
def fac(n) when n > 0, do: n * fac(n - 1)
|
||||
|
||||
# Tail recursive function
|
||||
def fac_tail(n), do: fac_tail(n, 1)
|
||||
def fac_tail(0, acc), do: acc
|
||||
def fac_tail(n, acc) when n > 0, do: fac_tail(n - 1, acc * n)
|
||||
|
||||
# Using Enumeration features
|
||||
def fac_reduce(0), do: 0
|
||||
def fac_reduce(n) when n > 0, do: Enum.reduce(1..n, 1, &*/2)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue