Last active
August 29, 2015 13:56
-
-
Save philss/8812743 to your computer and use it in GitHub Desktop.
A simple recursion example using pattern matching.
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 Math do | |
def sum_list([number]) do | |
number | |
end | |
def sum_list([head|tail]) do | |
head + sum_list(tail) | |
end | |
end | |
# Examples: | |
# Math.sum_list([10]) | |
# # => 10 | |
# | |
# Math.sum_list([17, 13, 7, 5]) | |
# # => 42 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment