Skip to content

Instantly share code, notes, and snippets.

@jaen
Created September 7, 2013 12:11
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 jaen/6475074 to your computer and use it in GitHub Desktop.
Save jaen/6475074 to your computer and use it in GitHub Desktop.
defmodule Test do
def sum(list), do: _sum(list, 0)
defp _sum([], acc), do: acc
defp _sum([x|xs], acc), do: _sum(xs, acc+x)
def max(list), do: _max(list, Enum.first(list))
defp _max([], max), do: max
defp _max([x|xs], max)
when x > max, do: _max(xs, x)
defp _max([x|xs], max), do: _max(xs, max)
end
sum list = sum' list 0
where sum' [] acc = acc
sum' (x:xs) acc = sum' xs acc+x
max list = max' list (head list)
where max' [] max = max
max' (x:xs) max
| x > max = max' xs x
| otherwise = max' xs max
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment