-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmylist.exs
32 lines (23 loc) · 861 Bytes
/
mylist.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
defmodule MyList do
def len([]), do: 0
def len([_head|tail]), do: 1 + len(tail)
def square([]), do: []
def square([ head | tail ]), do: [ head * head | square(tail)]
def add_1([]), do: []
def add_1([ head | tail ]), do: [head + 1 | add_1(tail)]
def map([], _func), do: []
def map([ head | tail ], func ), do: [func.(head) | map(tail, func)]
def reduce([], value, _), do: value
def reduce([ head | tail ], value, func) do
reduce(tail, func.(head,value), func)
end
def mapsum([], _fun), do: 0
def mapsum([head | tail ], fun) do
fun.(head) + mapsum(tail, fun)
end
def max([n]), do: n
def max([head|tail]), do: Kernel.max(head,max(tail))
def caesar([], _n), do: []
def caesar([head |tail], n) when head+n <= ?z, do: [head+n| caesar(tail,n)]
def caesar([head|tail], n), do: [head+n-26| caesar(tail, n)]
end