Skip to content

Instantly share code, notes, and snippets.

@craigp
Created October 9, 2014 19:54
Show Gist options
  • Save craigp/7a0b446a449b943d1ea0 to your computer and use it in GitHub Desktop.
Save craigp/7a0b446a449b943d1ea0 to your computer and use it in GitHub Desktop.
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