在 Erlang 中使用 case 子句而不是函数子句实现列表:映射
谁能告诉我这是什么意思?我对此很陌生,我的朋友推荐我在这个网站上发帖。 顺便说一句,我是 Erlang 新手。
如果可能的话,我想在编辑器中编写代码,但我什至不理解任何示例输入/输出以及解释如何工作的问题。谢谢
Can anyone tell me what this means? I am new to this and my friend recommended me to post in this website.
By the way I'm new to Erlang.
If possible I want to write a code in editor and I don't even understand the question any sample input/output and how it works an explanation will do. Thankyou
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,问题是指
列表的实现:map/2
,将相同函数(接收到参数)应用于列表的所有元素并返回结果列表的函数。换句话说,此功能。
您可以检查要查看该函数的实现方式:
或者您可以构想一个偶数 simpler 实现,因为
它们俩都是……(对于OTP版本,我指的是
map_1/2 < /code>)在功能子句中使用模式匹配,以区分基本情况和函数的递归步骤。
您收到的请求是使用带有
case> Case>子句的单个函数子句实现相同的算法,而不是您在上面看到的两个函数条款。
It seems to me that the question refers to the implementation of
lists:map/2
, a function that applies the same function (received as a parameter) to all elements of a list and returns the resulting list.In other words, this function.
You can check the OTP Github repo to see how that function is implemented:
Or you can conceive an even simpler implementation, as…
Both of them (for the OTP version, I'm referring to
map_1/2
) use pattern-matching in function clause heads to distinguish between the base case and the recursive step of the function.The request that you received is to implement the same algorithm using a single function clause with a
case
clause instead of the two function clauses you see above.下面是一个简单的示例,展示了如何使用函数子句,然后使用 case 语句来执行相同的操作。将以下代码放入某个目录中名为
a.erl
的文件中:请注意,文件名
a.erl
和模块指令:必须匹配。因此,如果您将文件命名为
homework1.erl
,那么文件中的模块指令必须是:为了节省大量输入,最好使用非常短的模块名称(如下所示) 。
在终端窗口中,将目录切换到包含
a.erl
的目录:然后启动 erlang shell:
接下来,执行以下语句:
注意调用文件/模块中定义的函数的语法:
Here's a simple example showing how to use function clauses, then case statements to do the same thing. Put the following code in a file named
a.erl
in some directory:Note that the file name,
a.erl
and the module directive:must match. So, if you named your file
homework1.erl
, then the module directive in the file must be:To save a lot of typing, it's best to use very short module names (as you will see below).
In a terminal window, switch directories to the directory containing
a.erl
:then launch the erlang shell:
Next, execute the following statements:
Note the syntax for calling a function defined in a file/module:
在 Brujo Benavides 的答案中链接的文档中,您可以看到:
所以
F
是一个函数(单个参数),例如fun(X) -> X*2结束
。请参阅 https://www.erlang.org/doc/programming_examples /funs.html#syntax-of-funs 或 https://www.erlang.org/doc/reference_manual/expressions.html#funs 来理解fun
表达式。List1
是函数F
可以处理的值列表(在本例中为数字),例如[1,2,3]
。然后list:map(fun(X) -> X*2 end, [1,2,3])
调用fun(X) -> X*2 end, [1,2,3])
X*2 end 位于列表[1,2,3]
的每个元素上,并返回返回值列表[2,4,6]
。您的函数应该对这些参数给出相同的结果。In the documentaion linked in Brujo Benavides's answer, you can see:
So
F
is a function (of a single argument) such asfun(X) -> X*2 end
. See https://www.erlang.org/doc/programming_examples/funs.html#syntax-of-funs or https://www.erlang.org/doc/reference_manual/expressions.html#funs to understandfun
expressions.List1
is a list of values which the functionF
can work on (in this case numbers) such as[1,2,3]
. Thenlist:map(fun(X) -> X*2 end, [1,2,3])
callsfun(X) -> X*2 end
on each element of list[1,2,3]
and returns the list of return values[2,4,6]
. Your function should give the same result on these arguments.