7171```
7272
7373As shown, the function ` say_something ` writes its first argument the number of
74- times specified by second argument. The function ` start ` starts two Erlang
74+ times specified by the second argument. The function ` start ` starts two Erlang
7575processes, one that writes "hello" three times and one that writes "goodbye"
7676three times. Both processes use the function ` say_something ` . Notice that a
7777function used in this way by ` spawn ` , to start a process, must be exported from
@@ -149,7 +149,7 @@ start() ->
149149``` erlang
1501501 > c (tut15 ).
151151{ok ,tut15 }
152- 2 > tut15 : start ().
152+ 2 > tut15 :start ().
153153< 0.36 .0 >
154154Pong received ping
155155Ping received pong
@@ -205,7 +205,7 @@ receive
205205 pattern2 ->
206206 actions2 ;
207207 ....
208- patternN
208+ patternN ->
209209 actionsN
210210end .
211211```
@@ -254,7 +254,7 @@ process "ping":
254254Ping_PID ! pong
255255```
256256
257- Notice how the operator " \! " is used to send messages. The syntax of " \! " is:
257+ Notice how the operator ` ! ` is used to send messages. The syntax of ` ! ` is:
258258
259259``` erlang
260260Pid ! Message
@@ -283,7 +283,7 @@ Pong_PID ! {ping, self()},
283283```
284284
285285` self/0 ` returns the pid of the process that executes ` self/0 ` , in this case the
286- pid of "ping". (Recall the code for "pong", this lands up in the variable
286+ pid of "ping". (Recall the code for "pong", this ends up in the variable
287287` Ping_PID ` in the ` receive ` previously explained.)
288288
289289"Ping" now waits for a reply from "pong":
@@ -397,7 +397,7 @@ pong ! {ping, self()},
397397## Distributed Programming
398398
399399Let us rewrite the ping pong program with "ping" and "pong" on different
400- computers. First a few things are needed to set up to get this to work. The
400+ computers. First a few things need to be set up to get this to work. The
401401distributed Erlang implementation provides a very basic authentication mechanism
402402to prevent unintentional access to an Erlang system on another computer. Erlang
403403systems which talk to each other must have the same _magic cookie_. The easiest
@@ -438,7 +438,7 @@ Erlang system running on a computer is called an _Erlang node_.
438438
439439(Note: `erl -sname` assumes that all nodes are in the same IP domain and we can
440440use only the first component of the IP address, if we want to use nodes in
441- different domains we use `-name` instead, but then all IP address must be given
441+ different domains we use `-name` instead, but then all IP addresses must be given
442442in full.)
443443
444444Here is the ping pong example modified to run on two separate nodes:
@@ -483,20 +483,20 @@ started on kosken, called ping, and then a node on gollum, called pong.
483483On kosken (on a Linux/UNIX system):
484484
485485```text
486- kosken> erl -sname ping
487- Erlang (BEAM) emulator version 5.2.3.7 [hipe ] [threads:0 ]
486+ kosken$ erl -sname ping
487+ Erlang/OTP 28 [erts-16.3] [source ] [64-bit] [smp:16:16] [ds:16:16:10] [async- threads:1] [jit:ns ]
488488
489- Eshell V5.2.3.7 ( abort with ^G )
489+ Eshell V16.3 (press Ctrl+G to abort, type help(). for help )
490490(ping@kosken)1>
491491```
492492
493493On gollum:
494494
495495```text
496- gollum> erl -sname pong
497- Erlang (BEAM) emulator version 5.2.3.7 [hipe ] [threads:0 ]
496+ gollum$ erl -sname pong
497+ Erlang/OTP 28 [erts-16.3] [source ] [64-bit] [smp:16:16] [ds:16:16:10] [async- threads:1] [jit:ns ]
498498
499- Eshell V5.2.3.7 ( abort with ^G )
499+ Eshell V16.3 (press Ctrl+G to abort, type help(). for help )
500500(pong@gollum)1>
501501```
502502
@@ -533,7 +533,7 @@ Pong finished
533533
534534Looking at the `tut17` code, you see that the `pong` function itself is
535535unchanged, the following lines work in the same way irrespective of on which
536- node the "ping" process is executes :
536+ node the "ping" process is executed :
537537
538538```erlang
539539{ping, Ping_PID} ->
@@ -623,7 +623,7 @@ Before starting, notice the following:
623623- This example only shows the message passing logic - no attempt has been made
624624 to provide a nice graphical user interface, although this can also be done in
625625 Erlang.
626- - This sort of problem can be solved easier by use of the facilities in OTP,
626+ - This sort of problem can be solved more easily by using the facilities in OTP,
627627 which also provide methods for updating code on the fly and so on (see
628628 [OTP Design Principles](`e:system:design_principles.md`)).
629629- The first program contains some inadequacies regarding handling of nodes which
@@ -666,9 +666,6 @@ File `messenger.erl`:
666666%%% Reply {messenger, logged_on} logon was successful
667667%%%
668668%%% To server: {ClientPid, logoff}
669- %%% Reply: {messenger, logged_off}
670- %%%
671- %%% To server: {ClientPid, logoff}
672669%%% Reply: no reply
673670%%%
674671%%% To server: {ClientPid, message_to, ToName, Message} send a message
@@ -697,7 +694,7 @@ server_node() ->
697694 messenger@super.
698695
699696%%% This is the server process for the "messenger"
700- %%% the user list has the format [{ClientPid1, Name1},{ClientPid22 , Name2},...]
697+ %%% the user list has the format [{ClientPid1, Name1},{ClientPid2 , Name2},...]
701698server(User_List) ->
702699 receive
703700 {From, logon, Name} ->
@@ -734,22 +731,22 @@ server_logoff(From, User_List) ->
734731 lists:keydelete(From, 1, User_List).
735732
736733
737- %%% Server transfers a message between user
734+ %%% Server transfers a message between users
738735server_transfer(From, To, Message, User_List) ->
739736 %% check that the user is logged on and who he is
740- case lists:keysearch (From, 1, User_List) of
737+ case lists:keyfind (From, 1, User_List) of
741738 false ->
742739 From ! {messenger, stop, you_are_not_logged_on};
743- {value, { From, Name} } ->
740+ {From, Name} ->
744741 server_transfer(From, Name, To, Message, User_List)
745742 end.
746743%%% If the user exists, send the message
747744server_transfer(From, Name, To, Message, User_List) ->
748745 %% Find the receiver and send the message
749- case lists:keysearch (To, 2, User_List) of
746+ case lists:keyfind (To, 2, User_List) of
750747 false ->
751748 From ! {messenger, receiver_not_found};
752- {value, { ToPid, To} } ->
749+ {ToPid, To} ->
753750 ToPid ! {message_from, Name, Message},
754751 From ! {messenger, sent}
755752 end.
@@ -894,7 +891,7 @@ call. This would result in the process getting bigger and bigger for every loop.
894891
895892Functions in the `lists` module are used. This is a very useful module and a
896893study of the manual page is recommended (`erl -man lists`).
897- `lists:keymember(Key,Position,Lists )` looks through a list of tuples and looks
894+ `lists:keymember(Key,Position,List )` looks through a list of tuples and looks
898895at `Position` in each tuple to see if it is the same as `Key`. The first element
899896is position 1. If it finds a tuple where the element at `Position` is the same
900897as `Key`, it returns `true`, otherwise `false`.
@@ -914,8 +911,8 @@ any) and returns the remaining list:
914911[{x,y,z},{b,b,b},{q,r,s}]
915912```
916913
917- `lists:keysearch ` is like `lists:keymember`, but it returns
918- `{value, Tuple_Found} ` or the atom `false`.
914+ `lists:keyfind ` is like `lists:keymember`, but it returns
915+ `Tuple_Found` or the atom `false`.
919916
920917There are many very useful functions in the `lists` module.
921918
@@ -974,19 +971,19 @@ server_transfer(From, fred, "hello", User_List),
974971This checks that the pid `From` is in the `User_List`:
975972
976973```erlang
977- lists:keysearch (From, 1, User_List)
974+ lists:keyfind (From, 1, User_List)
978975```
979976
980- If `keysearch ` returns the atom `false`, some error has occurred and the server
977+ If `keyfind ` returns the atom `false`, some error has occurred and the server
981978sends back the message:
982979
983980```erlang
984981From ! {messenger, stop, you_are_not_logged_on}
985982```
986983
987984This is received by the client, which in turn does [`exit(normal)`](`exit/1`)
988- and terminates. If `keysearch ` returns `{value,{ From,Name} }` it is certain that
989- the user is logged on and that his name (peter) is in variable `Name`.
985+ and terminates. If `keyfind ` returns `{From,Name}` it is certain that
986+ the user is logged on and that their name (peter) is in the variable `Name`.
990987
991988Let us now call:
992989
@@ -995,11 +992,11 @@ server_transfer(From, peter, fred, "hello", User_List)
995992```
996993
997994Notice that as this is `server_transfer/5`, it is not the same as the previous
998- function `server_transfer/4`. Another `keysearch ` is done on `User_List` to find
995+ function `server_transfer/4`. Another `keyfind ` is done on `User_List` to find
999996the pid of the client corresponding to fred:
1000997
1001998```erlang
1002- lists:keysearch (fred, 2, User_List)
999+ lists:keyfind (fred, 2, User_List)
10031000```
10041001
10051002This time argument 2 is used, which is the second element in the tuple. If this
@@ -1012,10 +1009,10 @@ From ! {messenger, receiver_not_found};
10121009
10131010This is received by the client.
10141011
1015- If `keysearch ` returns:
1012+ If `keyfind ` returns:
10161013
10171014```erlang
1018- {value, { ToPid, fred} }
1015+ {ToPid, fred}
10191016```
10201017
10211018The following message is sent to fred' s client :
0 commit comments