其他分享
首页 > 其他分享> > elixir 二:循环list求和

elixir 二:循环list求和

作者:互联网

 1 defmodule DemoTest do
 2   def f do
 3     IO.puts("cccc")
 4   end
 5 
 6   def testRecursion([], n) do
 7     n
 8   end
 9 
10   def testRecursion(msg, n) do
11     testRecursion(tl(msg), hd(msg) + n)
12   end
13 end
14 
15 a = [1,2,3]
16 b=DemoTest.testRecursion(a, 0)
17 IO.puts(b)

 

 

官方例子:

1  def sum_list([head | tail], accumulator) do
2     sum_list(tail, head + accumulator)
3   end
4 
5   def sum_list([], accumulator) do
6     accumulator
7   end

 

标签:do,end,求和,list,accumulator,elixir,testRecursion,def
来源: https://www.cnblogs.com/sun-shadow/p/16388858.html