annotate 5-Erlang/translate.erl @ 75:2df568aae515

Add translation example with notes
author IBBoard <dev@ibboard.co.uk>
date Sat, 03 Feb 2018 20:12:07 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
75
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 -module(translate).
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 -export([loop/0]).
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 % Invoke with: Pid = spawn(fun translate:loop/0).
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 % Send messages with: Pid ! "message".
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 % This is async with no return value.
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 loop() ->
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 receive
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 % "receive" is a control structure that defines what "messages" we respond to
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 "casa" ->
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 io:format("house~n"),
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 loop(); % And each entry must make sure that we start the loop again
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 % If we had a "quit" message then it just wouldn't call loop()
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 % Because of the way that tail recursion works then calling this last
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 % has almost no overhead, but doing it in the middle would.
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 "blanca" ->
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 io:format("white~n"),
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 loop();
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 _ ->
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 io:format("*blank stare*~n"),
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 loop()
2df568aae515 Add translation example with notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 end.