Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -4,11 +4,12 @@ defmodule Factorial do
|
|||
def fac(n) when n > 0, do: n * fac(n - 1)
|
||||
|
||||
# Tail recursive function
|
||||
def fac_tail(0), do: 1
|
||||
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)
|
||||
def fac_tail(1, acc), do: acc
|
||||
def fac_tail(n, acc) when n > 1, do: fac_tail(n - 1, acc * n)
|
||||
|
||||
# Using Enumeration features
|
||||
def fac_reduce(0), do: 0
|
||||
def fac_reduce(0), do: 1
|
||||
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