在 Erlang 中使用 case 子句而不是函数子句实现列表:映射

发布于 2025-01-18 19:07:36 字数 119 浏览 5 评论 0原文

谁能告诉我这是什么意思?我对此很陌生,我的朋友推荐我在这个网站上发帖。 顺便说一句,我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽梦忆笙歌 2025-01-25 19:07:36

在我看来,问题是指列表的实现:map/2,将相同函数(接收到参数)应用于列表的所有元素并返回结果列表的函数。

换句话说,此功能

您可以检查要查看该函数的实现方式:

map(F, List) when is_function(F, 1) ->
    case List of
        [Hd | Tail] -> [F(Hd) | map_1(F, Tail)];
        [] -> []
    end.

map_1(F, [Hd | Tail]) ->
    [F(Hd) | map_1(F, Tail)];
map_1(_F, []) ->
    [].

或者您可以构想一个偶数 simpler 实现,因为

map(F, []) -> [];
map(F, [H|T]) -> [F(H) | map(F, T)].

它们俩都是……(对于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:

map(F, List) when is_function(F, 1) ->
    case List of
        [Hd | Tail] -> [F(Hd) | map_1(F, Tail)];
        [] -> []
    end.

map_1(F, [Hd | Tail]) ->
    [F(Hd) | map_1(F, Tail)];
map_1(_F, []) ->
    [].

Or you can conceive an even simpler implementation, as…

map(F, []) -> [];
map(F, [H|T]) -> [F(H) | map(F, T)].

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.

安人多梦 2025-01-25 19:07:36

下面是一个简单的示例,展示了如何使用函数子句,然后使用 case 语句来执行相同的操作。将以下代码放入某个目录中名为 a.erl 的文件中:

-module(a).
-export([show_stuff/1, show_it/1]).

show_stuff(1) ->
    io:format("The argument was 1~n");
show_stuff(2) ->
    io:format("The argument was 2~n");
show_stuff(_)->
    io:format("The argument was something other than 1 or 2~n").

show_it(X) ->
    case X of
        1 -> io:format("The argument was 1~n");
        2 -> io:format("The argument was 2~n");
        _ -> io:format("The argument was something other than 1 or 2~n")
    end.

请注意,文件名 a.erl 和模块指令:

-module(a).

必须匹配。因此,如果您将文件命名为 homework1.erl,那么文件中的模块指令必须是:

-module(homework1).

为了节省大量输入,最好使用非常短的模块名称(如下所示) 。

在终端窗口中,将目录切换到包含 a.erl 的目录:

 ~$ cd erlang_programs/

然后启动 erlang shell:

 ~/erlang_programs$ erl
Erlang/OTP 24 [erts-12.0.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]

Eshell V12.0.2  (abort with ^G)

接下来,执行以下语句:

1> c(a).   <--- Compiles the code in your file 
{ok,a}     <--- Or, you may get errors which must be corrected, then try recompiling.

2> a:show_stuff(1).
The argument was 1
ok

3> a:show_stuff(4).
The argument was something other than 1 or 2
ok

4> a:show_it(1).
The argument was 1
ok

5> a:show_it(4).
The argument was something other than 1 or 2
ok

6> 

注意调用文件/模块中定义的函数的语法:

 module_name:function_name(arg1, arg2, ... argn).

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:

-module(a).
-export([show_stuff/1, show_it/1]).

show_stuff(1) ->
    io:format("The argument was 1~n");
show_stuff(2) ->
    io:format("The argument was 2~n");
show_stuff(_)->
    io:format("The argument was something other than 1 or 2~n").

show_it(X) ->
    case X of
        1 -> io:format("The argument was 1~n");
        2 -> io:format("The argument was 2~n");
        _ -> io:format("The argument was something other than 1 or 2~n")
    end.

Note that the file name, a.erl and the module directive:

-module(a).

must match. So, if you named your file homework1.erl, then the module directive in the file must be:

-module(homework1).

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:

 ~$ cd erlang_programs/

then launch the erlang shell:

 ~/erlang_programs$ erl
Erlang/OTP 24 [erts-12.0.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]

Eshell V12.0.2  (abort with ^G)

Next, execute the following statements:

1> c(a).   <--- Compiles the code in your file 
{ok,a}     <--- Or, you may get errors which must be corrected, then try recompiling.

2> a:show_stuff(1).
The argument was 1
ok

3> a:show_stuff(4).
The argument was something other than 1 or 2
ok

4> a:show_it(1).
The argument was 1
ok

5> a:show_it(4).
The argument was something other than 1 or 2
ok

6> 

Note the syntax for calling a function defined in a file/module:

 module_name:function_name(arg1, arg2, ... argn).
薄凉少年不暖心 2025-01-25 19:07:36

任何示例输入/输出及其工作原理的解释都可以

在 Brujo Benavides 的答案中链接的文档中,您可以看到:

<块引用>

采用从 As 到 Bs 的函数以及 As 列表,并通过将该函数应用于列表中的每个元素来生成 Bs 列表。该函数用于获取返回值。

所以 F 是一个函数(单个参数),例如 fun(X) -> X*2结束。请参阅 https://www.erlang.org/doc/programming_examples /funs.html#syntax-of-funshttps://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]。您的函数应该对这些参数给出相同的结果。

any sample input/output and how it works an explanation will do

In the documentaion linked in Brujo Benavides's answer, you can see:

Takes a function from As to Bs, and a list of As and produces a list of Bs by applying the function to every element in the list. This function is used to obtain the return values.

So F is a function (of a single argument) such as fun(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 understand fun expressions. List1 is a list of values which the function F can work on (in this case numbers) such as [1,2,3]. Then list:map(fun(X) -> X*2 end, [1,2,3]) calls fun(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文