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.
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: