Skip to content

Instantly share code, notes, and snippets.

@philss
Last active August 29, 2015 13:56
Show Gist options
  • Save philss/8812743 to your computer and use it in GitHub Desktop.
Save philss/8812743 to your computer and use it in GitHub Desktop.
A simple recursion example using pattern matching.
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