Recursion and anonymous functions in elixir

前端 未结 4 1926
我寻月下人不归
我寻月下人不归 2021-02-03 19:52

I\'m trying to define an anonymous function to do a dot product, I can code this as a private function without any problem but I am struggling with the anonymous function syntax

4条回答
  •  被撕碎了的回忆
    2021-02-03 20:31

    The less formal but still acceptable approach is:

    factorial = fn
      (0,_) -> 1
      (1,_) -> 1
      (n, fun) -> n * fun.(n - 1, fun)
    end
    

    You call it with factorial.(6, factorial) # 720

提交回复
热议问题