Question: Erlang language - Create a module file named functions4.erl . In this module, write the following functions. Make sure your module name is functions4 ,
Erlang language -
Create a module file named functions4.erl. In this module, write the following functions. Make sure your module name is functions4, and that you export each of these functions.
1. Write an Erlang function named send_message that takes two arguments. You may assume that the first argument is a process ID, and the second argument can be any Erlang term. The function should send the second argument to the process ID given in the first argument.
2. Write an Erlang function named print_message that takes no arguments. The function should wait to receive a message. When the message is received (it can be any Erlang term), print the message using io:format(). If 42 seconds pass without receiving a message, print a message that says Too late..
3. Write an Erlang function named calculator that takes no arguments. The function, when run in a process, should wait to receive a message. If the message is in the form of a tuple of four items: { Pid, Operand, X, Y }, do this:
- If Operand is add, send a message to Pid with the result of adding X and Y.
- If Operand is subtract, send a message to Pid with the result of subtracting Y from X.
- If Operand is multiply, send a message to Pid with the result of multiplying X and Y.
- If Operand is divide, send a message to Pid with the result of dividing X by Y.
Then, rerun the function.
If the message is in the form of a tuple with two items: { Pid, terminate }, then send a message to Pid of done. Do not rerun the function.
If the message is of any other form, ignore it and rerun the function.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
