编写一个程序来产生一个新过程,该过程会收回X整数并返回X+ 1。如果在Erlang中传递了非整数消息,它将退出吗?
我是Erlang的新手,我已经完成了代码,但是我需要此问题的正确答案格式。这是我的代码。如果我输入任何整数,则必须将1添加到该数字。如果给出了字符或浮点或任何其他非整数,则应从该过程中退出。
-module(a).
-export([start/0,func1/1]).
func1(List) when is_integer(List) -> io:format("~p~n",[List+1]);
func1(List) when is_atom(List) -> io:format("Exit~n");
func1(List) when is_binary(List) -> io:format("Exit~n");
func1(List) when is_list(List) -> io:format("Exit~n");
func1(List) when is_map(List) -> io:format("Exit~n");
func1(List) when is_float(List) -> io:format("Exit~n").
start() ->
spawn(a, func1, [45]),
spawn(a, func1, [test]).
此代码正常运行,但我需要以ping pong格式进行此操作,例如接收和结束,例如传递消息。请让我知道正确的答案,并提前感谢。
这是我看评论后尝试的。
-module(a).
-compile(export_all).
f1() ->
receive
List ->
io:format("~p~n", [List+1]);
_ ->
io:format("Exit~n")
end.
f2() ->
receive
{From, List} ->
From ! "~p~n" ,[List+1];
_ ->
io:format("Exit~n")
end.
f3() ->
receive
{From, List} ->
From ! "~p~n" , [List+1],
f3();
_ ->
io:format("Exit~n"),
f3()
end.
I am new to erlang and I have done the code, But I need the right answer format for this question. Here's my code. If I enter any Integers then it'll have to add 1 to that number. If character or float or any other non integer is given It should exit from the process.
-module(a).
-export([start/0,func1/1]).
func1(List) when is_integer(List) -> io:format("~p~n",[List+1]);
func1(List) when is_atom(List) -> io:format("Exit~n");
func1(List) when is_binary(List) -> io:format("Exit~n");
func1(List) when is_list(List) -> io:format("Exit~n");
func1(List) when is_map(List) -> io:format("Exit~n");
func1(List) when is_float(List) -> io:format("Exit~n").
start() ->
spawn(a, func1, [45]),
spawn(a, func1, [test]).
This code works fine But I need to do this in ping pong format something like receive and end like passing messages. Please let me know the right answer and thanks in advance.
here's what I tried after looking at the comments.
-module(a).
-compile(export_all).
f1() ->
receive
List ->
io:format("~p~n", [List+1]);
_ ->
io:format("Exit~n")
end.
f2() ->
receive
{From, List} ->
From ! "~p~n" ,[List+1];
_ ->
io:format("Exit~n")
end.
f3() ->
receive
{From, List} ->
From ! "~p~n" , [List+1],
f3();
_ ->
io:format("Exit~n"),
f3()
end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您不必为不是整数的每个类型定义函数子句。关键是要认识到函数从句按顺序进行测试。您的第一个函数条款测试该参数是整数。这意味着,如果Erlang到达您的第二个功能条款,那么您知道该参数不是整数 - 无需进行任何类型检查。因此,为第二个函数条款中的所有非直集参数做需要做的事情。
其次,命名一个参数
列表
,当参数可以是整数,原子,元组,二进制等方面是非敏感的。然后,您需要启动其他过程而不发送任何参数,然后使另一个过程输入递归循环/函数。您在递归循环/函数中需要的唯一语句是
接收语句
,它将停止直到收到消息。您的主要过程可以使用
send()
将消息发送到另一个过程。您将需要定义一个用户函数,例如regrement_number(number)
。内部enrecement_number(number)
,您将调用send> send()
将消息传输到另一个过程,然后下一个语句将是接收,它将等待返回来自另一个过程的消息。所有开始的Erlang编程书都告诉您如何创建多个相互交流的过程。我建议“编程erlang” - 阅读有关并发的相关部分。
您将不得不花很多时间阅读基本的并发示例,与之一起玩,然后尝试适应自己的使用。
通常,消息将作为元组传递,例如
或等效:
另一个过程与接收子句中的消息匹配:
执行某些任务后,接收过程需要
send()
一条消息回到发送过程,即包含一些算术计算结果的消息。而且,由于您希望接收过程能够处理不确定的消息和随后的计算,因此您要创建一个无限的循环。您在Erlang中这样做的方式具有递归功能:First of all, you don't have to define a function clause for every type that is not an integer. The key is to realize that function clauses are tested IN ORDER. Your first function clause tests if the argument is an integer. That means if erlang gets to your second function clause, then you know the argument is NOT an integer--without having to do ANY type checking. So, do what you need to do for all non-integer arguments in the second function clause.
Second, naming an argument
List
, when the argument can be an integer, an atom, a tuple, a binary, etc. is non-sensical.Then, you need to start the other process without sending it any argument, then make the other process enter a recursive loop/function. The only statement you need in the recursive loop/function is a
receive statement
, which will halt until it receives a message.Your main process can use
send()
to send messages to the other process. You will want to define a user function likeincrement_number(Number)
. Insideincrement_number(Number)
, you will callsend()
to transmit a message to the other process, then the next statement will be a receive, which will wait for a return message from the other process.All beginning erlang programming books tell you how to create multiple processes that talk to each other. I recommend "Programming Erlang"--read the relevant sections on concurrency.
You are going to have to spend many, many hours reading basic concurrency examples, playing with them, and then trying to adapt them for your own use.
Typically, message are passed as tuples, e.g.
or, equivalently:
The other process then matches the messages in a receive clause:
After performing some task, the receiving process needs to
send()
a message back to the sending process, i.e. a message containing the results of some arithmetic calculation. And, because you want the receiving process to be able to handle an indeterminate number of messages and subsequent calculations, you want to create an infinite loop. The way you do that in erlang is with a recursive function: