September 2013
Intermediate to advanced
548 pages
12h 25m
English
Take a look at the receive loop in the area server that we wrote earlier:
| area_server_final.erl | |
| | loop() -> |
| | receive |
| | {From, {rectangle, Width, Ht}} -> |
| | From ! {self(), Width * Ht}, |
| | loop(); |
| | {From, {circle, R}} -> |
| | From ! {self(), 3.14159 * R * R}, |
| | loop(); |
| | {From, Other} -> |
| | From ! {self(), {error,Other}}, |
| | loop() |
| | end. |
If you look carefully, you’ll see that every time we receive a
message, we process the message and then immediately call
loop() again. Such a procedure is called
tail-recursive. A tail-recursive function can be compiled so that the last function call in a sequence of statements can be replaced by a simple jump to the start of the function being called. This means that a tail-recursive ...
Read now
Unlock full access