pattern_matching = "I am your friend"
"I am your friend"
"I am your friend" = pattern_matching
"I am your friend"
{atom, string, charlist, list} = {:i_am_atom, "string", 'charlist', []}
{:i_am_atom, "string", 'charlist', []}
atom === :i_am_atom
true
string === "string"
true
charlist === 'charlist'
true
list === []
true
list = ["fisrt", "second", "third"]
["fisrt", "second", "third"]
[head | tail] = list
["fisrt", "second", "third"]
head === "first"
true
tail === ["second", "third"]
true
當參數只有一個的時候
def show_msg(name), do: IO.puts "I'm #{name}. I'm not in a team. I am the team!"
當參數有兩個的時候
def show_msg(name1, name2), do: IO.puts "We are #{name1} and #{name2}. We build the team."
當參數有兩個時候,但有條件
def show_msg(name1, _) when name1 === "Johnson" do
IO.puts "We build the team! I'm #{name1} and the rest."
end
defmodule Meme do
def show_msg(name), do: IO.puts "I'm #{name}. I'm not in a team. I am the team!"
def show_msg(name1, name2), do: IO.puts "We are #{name1} and #{name2}. We build the team."
def show_msg(name1, _) when name1 === "Johnson" do
IO.puts "We build the team! I'm #{name1} and the rest."
end
end