Elixir模式匹配功能
示例
#You can use pattern matching to run different
#functions based on which parameters you pass
#This example uses pattern matching to start,
#run, and end a recursive function
defmodule Counter do
def count_to do
count_to(100, 0) #No argument, init with 100
end
def count_to(counter) do
count_to(counter, 0) #Initialize the recursive function
end
def count_to(counter, value) when value == counter do
#This guard clause allows me to check my arguments against
#expressions. This ends the recursion when the value matches
#the number I am counting to.
:ok
end
def count_to(counter, value) do
#Actually do the counting
IO.putsvalue
count_to(counter, value + 1)
end
end
热门推荐
10 事业祝福语简短网名
11 节日祝福语大全简短创意
12 牛年企业祝福语简短大气
13 学校生日祝福语简短独特
14 古风祝福语 唯美简短结婚
15 朋友新工作祝福语简短
16 写给女友的祝福语++简短
17 新年至客户祝福语简短
18 圣诞祝福语大全简短星座