Last active
October 15, 2015 17:18
-
-
Save mzemel/38dbfd2d82c492192e4c to your computer and use it in GitHub Desktop.
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 D do | |
def add(a, b, c), do: a + b + c # Arity 3 | |
def add(b, b), do: 3 * b # Pattern matching | |
def add("i", b), do: "#{b}i" # Pattern matching | |
def add(a, _), do: a + a # Strange default, but we'll go with it | |
def add(_), do: IO.puts "Can't add one number, silly" | |
end | |
D.add(1, 2, 3) | |
# => 6 | |
D.add(5, 5) | |
# => 15 | |
D.add("i", 10) | |
# => 10i | |
D.add(3, 4) | |
# => 6 | |
D.add(10) | |
# => "Can't add one number, silly" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment