-
Notifications
You must be signed in to change notification settings - Fork 4
/
lists_and_recursion_5.exs
41 lines (37 loc) · 1020 Bytes
/
lists_and_recursion_5.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
defmodule MyList do
def all?([], func), do: true
def all?([head | tail], func) do
if func.(head) do
all?(tail, func)
else
false
end
end
def each([], _function), do: :ok
def each([head | tail], function) do
function.(head)
each(tail, function)
end
def filter([], _func), do: []
def filter([head | tail], func) do
if func.(head) do
[head | filter(tail, func)]
else
filter(tail, func)
end
end
def split(list, count), do: _split(list, {[], []}, count)
def _split([], tuple, _count), do: tuple
def _split(list, tuple, 0) do
{elem(tuple, 0), list}
end
def _split([head | tail], tuple, count) do
_split(tail, {elem(tuple, 0) ++ [head], elem(tuple, 1)}, count - 1)
end
def take(list, count), do: _take(list, [], count)
def _take([], taken_list, _count), do: taken_list
def _take(list, taken_list, 0), do: taken_list
def _take([head | tail], taken_list, count) do
_take(tail, taken_list ++ [head], count - 1)
end
end