Created
April 24, 2020 04:24
-
-
Save emmanuelsw/3c6a57d26a79f3f61fa90587890ea9f3 to your computer and use it in GitHub Desktop.
lists.ex
This file contains hidden or 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 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