Erlang出现错误** 1:之前的语法错误:'->' ** [英] Erlang getting error ** 1: syntax error before: '->' **

查看:110
本文介绍了Erlang出现错误** 1:之前的语法错误:'->' **的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用Erlang了,并且得到了: ** 1:每当我声明任何函数时,语法错误:'->'** 例如计算列表的总和(这是实验性的,当然,内置函数可以找到列表的总和)。

I have started some hands on in Erlang and I am getting : ** 1: syntax error before: '->' ** whenever i am declaring any function for eg. to calculate sum of a list (this is experimental, of cource there is Built In Function for find sum of a list).

sum([]) -> 0;
sum([H | T]) -> H + sum(T).

在erl shell(v 5.5.5)中。

in erl shell (v 5.5.5).

预先感谢

推荐答案

您无法使用与erl文件中相同的语法在shell中定义函数

You can't define functions in the shell using the same syntax as in an erl file.

您可以定义乐趣。

shell中的语法必须为:

Syntax in the shell needs to be:

Sum = fun([], _) -> 0; ([H | T], F) -> H + F(T, F) end,
Sum([1,2,3], Sum).

请注意,递归匿名函数(这是一个丑陋的定义)。您基本上必须将函数作为参数传递给自身。

Note that recursive anonymous functions (which this is) are defined in an ugly way. You basically have to pass the function as an argument to itself.

这篇关于Erlang出现错误** 1:之前的语法错误:'->' **的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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