Created
October 9, 2014 19:54
-
-
Save craigp/7a0b446a449b943d1ea0 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 ExList do | |
def span(from, to) when from < to do | |
_span([], from, to) | |
end | |
defp _span(list, from, to) when from > to do | |
list | |
end | |
defp _span(list, from, to) when from <= to do | |
_span(list ++ [from], from + 1, to) | |
end | |
def max(list) do | |
_max(list, 0) | |
end | |
defp _max([], highest) do | |
highest | |
end | |
defp _max([head|tail], highest) when highest >= head do | |
_max(tail, highest) | |
end | |
defp _max([head|tail], highest) when head > highest do | |
_max(tail, head) | |
end | |
def mapsum(list, fun) do | |
_mapsum(list, fun, 0) | |
end | |
defp _mapsum([], _, total) do | |
total | |
end | |
defp _mapsum([head | tail], fun, total) do | |
_mapsum(tail, fun, fun.(head) + total) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment