Created
September 7, 2013 12:11
-
-
Save jaen/6475074 to your computer and use it in GitHub Desktop.
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 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 |
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
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