Last active
March 31, 2017 12:30
-
-
Save javiervidal/83e24bb12ceeefd0fa3501c3c55dd01c to your computer and use it in GitHub Desktop.
Lists and Recursion
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 MyList do | |
def map([], _func), do: [] | |
def map([ head | tail ], func), do: [ func.(head) | map(tail, func) ] | |
end | |
MyList.map [1,2,3,4], fn (n) -> n*n end | |
MyList.map [1,2,3,4], &(&1 * &1) |
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 MyList do | |
def reduce([], value, _) do | |
value | |
end | |
def reduce([head | tail], value, func) do | |
reduce(tail, func.(head, value), func) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment