-
-
Save ddombrow/f23da7532dfe5b7b7b06b578421b16ce to your computer and use it in GitHub Desktop.
elixirplayground.com code share
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule DeLists do | |
@moduledoc """ | |
This is the Delists module, simple code for simple list operations | |
""" | |
@doc """ | |
Sums a list recursively. | |
Returns the sum of the list. | |
""" | |
def sum(list) do | |
case list do | |
[head | tail] -> head + sum(tail) | |
_ -> 0 | |
end | |
end | |
@doc """ | |
Gets the highest value in a list | |
Returns the max element in a list | |
""" | |
def max(list) do | |
case list do | |
[head | tail] -> gt(head, max(tail)) | |
_ -> 0 | |
end | |
end | |
defp gt(a, b) do | |
case a do | |
a when (a > b) -> a | |
a when (b > a) -> b | |
_ -> a | |
end | |
end | |
end | |
IO.inspect(DeLists.max([1,20,2,4,5,9])) | |
IO.inspect(DeLists.sum([1,2,6,7])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment