-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamed_functions.exs
64 lines (55 loc) · 1.36 KB
/
named_functions.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
defmodule Times do
def double(n) do
n * 2
end
def doubleShort(n), do: n * 2
def tribble(n), do: n * 3
def quadruple(n), do: double(n) + double(n)
def quadrupleOther(n), do: n |> double() |> double()
end
defmodule Greetings do
def greeting(greeting, name), do: (
IO.puts greeting
IO.puts "How are you #{name}?"
)
end
defmodule Factorial do
def of(0), do: 1
def of(n), do: n * of(n-1)
end
defmodule FactorialGuard do
def of(0), do: 1
def of(n) when is_integer(n) and n > 0 do
n * of(n-1)
end
end
defmodule SumNaturals do
def sum(0), do: 0
def sum(n), do: n + sum(n - 1)
end
defmodule GreatestCommonDivisor do
def gcd(x,0), do: x
def gcd(x,y), do: gcd(y, rem(x,y))
end
defmodule DefaultParams do
def defaultParamsFunc(a, b \\ 2, c \\ 3, d), do: IO.inspect [a, b, c, d]
end
defmodule ChopExcercise do
def guess(input, min..max) do (
middle = div(min+max, 2)
IO.puts "Is it #{middle}?"
compare input, min..max, middle
)
end
defp compare(input, _.._, input), do: IO.puts "Yes it`s #{input}"
defp compare(input, min.._, current) when input < current do
guess input, min..current
end
defp compare(input, _..max, current) when input >= current do
guess input, current..max
end
end
defmodule AttributesExample do
@author "Florian Drews"
def get_author(), do: IO.puts "Examples written by #{@author}"
end