Elixir中的递归和匿名函数 [英] Recursion and anonymous functions in elixir
问题描述
我试图定义一个匿名函数来做一个点积,我可以毫无疑问地将其编码为一个私有函数,但是我在为匿名函数语法苦苦挣扎. 我知道我可以用不同的方法来实现,但是我试图理解如何通过模式匹配和递归来定义匿名函数. 这是我目前的实现方式
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. I know I could implement this differently but I am trying to understand how to define anonymous functions with pattern matching and recursion. This is my current implementation
dot = fn
[i|input],[w|weights], acc -> dot.(input,weights,i*w+acc)
[],[bias],acc -> acc + bias
end
在编译时出现此错误:
function dot/0 undefined
有任何提示吗?这不可能吗?
Any hints? Is this just not possible?
推荐答案
无法在Elixir中重复使用匿名函数.
It is not possible to recur on anonymous functions in Elixir.
Erlang 17(当前为候选版本)为Erlang添加了这种可能性,我们计划尽快利用它.现在,最好的方法是定义一个模块函数并传递给它:
Erlang 17 (currently a release candidate) adds this possibility to Erlang and we plan to leverage it soon. Right now, the best approach is to define a module function and pass it around:
def neural_bias([i|input],[w|weights], acc) do
neural(input,weights,i*w+acc)
end
def neural_bias([], [bias], acc) do
acc + bias
end
然后:
&neural_bias/3
这篇关于Elixir中的递归和匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!