Skip to content

Instantly share code, notes, and snippets.

@emmanuelsw
Created April 24, 2020 04:24
Show Gist options
  • Save emmanuelsw/3c6a57d26a79f3f61fa90587890ea9f3 to your computer and use it in GitHub Desktop.
Save emmanuelsw/3c6a57d26a79f3f61fa90587890ea9f3 to your computer and use it in GitHub Desktop.
lists.ex
defmodule Lists do
def len([]), do: 0
def len([_h|t]), do: 1 + len(t)
def sum([]), do: 0
def sum([h|t]), do: h + sum(t)
def double([]), do: []
def double([h|t]), do: [ 2*h | double(t) ]
def square([]), do: []
def square([h|t]), do: [ h*h | square(t) ]
def map([], _func), do: []
def map([h|t], func) do
[func.(h) | map(t, func)]
end
def sum_pairs([]), do: []
def sum_pairs([ h1, h2 | t]), do: [ h1 + h2 | sum_pairs(t) ]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment