Skip to content

Instantly share code, notes, and snippets.

@ddombrow
Forked from elixirplayground/code.exs
Created October 8, 2016 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddombrow/f23da7532dfe5b7b7b06b578421b16ce to your computer and use it in GitHub Desktop.
Save ddombrow/f23da7532dfe5b7b7b06b578421b16ce to your computer and use it in GitHub Desktop.
elixirplayground.com code share
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