Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
defmodule ListOps do
def count([]), do: 0
def count([h|t]), do: count(t) + 1
def reverse(l), do: reverse(l, [])
defp reverse([], acc), do: acc
defp reverse([h|t], acc), do: reverse(t, [h|acc])
def map([], _f), do: []
def map([h|t], f), do: [f.(h)|map(t, f)]
def filter([], _f), do: []
def filter([h|t], f) do
cond do
f.(h) -> [h|filter(t, f)]
true -> filter(t, f)
end
end
def reduce([], acc, f), do: acc
def reduce([h|t], acc, f), do: reduce(t, f.(h, acc), f)
def append([], []), do: []
def append([], l), do: l
def append([h|t], l), do: [h | append(t, l)]
def concat([]), do: []
def concat([h|t]), do: append(h, concat(t))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment