Elixir 中的递归和匿名函数 [英] Recursion and anonymous functions in elixir

查看:32
本文介绍了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

我在编译时遇到这个错误:

And I get this error on compile:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆