在 Erlang 中使用 case 子句而不是函数子句实现 lists:map

什里

谁能告诉我这是什么意思?我是新手,我的朋友推荐我在这个网站上发帖。顺便说一句,我是 Erlang 的新手。

如果可能的话,我想在编辑器中编写代码,但我什至不理解任何示例输入/输出的问题以及它是如何工作的解释。谢谢

7螺柱

这是一个简单的例子,展示了如何使用函数子句,然后使用 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).

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章