[erlang-bugs] Missing definitions in OTP Design Principles manual

Ulf Wiger ulf@REDACTED
Sun Oct 15 10:15:03 CEST 2006


Den 2006-10-15 02:22:58 skrev Kari Pahula <kaol@REDACTED>:

> I didn't find the definitions for alloc/1 and free/2 in OTP Design
> Principles manual version 5.5.1 (at
> http://www.erlang.org/doc/doc-5.5.1/doc/design_principles/part_frame.html)
> sections 1 and 2.
>
> For example, in section 1.2 there was this example:
>
> -module(ch1).
> -export([start/0]).
[...]
>
> It's not clear from this what alloc(Chs) and free(Ch, Chs) are
> supposed to do.

Well, the implementation of those functions isn't really
relevant to the example, and a realistic implementation would
probably also do something "interesting" when allocating and
freeing a channel. Nonetheless, one way to write these
functions, for completeness, would be:

channels() ->
   {_Allocated = [], _Free = lists:seq(1,100)}.

alloc({Allocated, [H|T] = _Free}) ->
   {H, {[H|Allocated], T]}}.

free(Ch, {Alloc, Free} = Channels) ->
   case lists:member(Ch, Alloc) of
      true ->
         {lists:delete(Ch, Alloc), [Ch|Free]};
      false ->
         Channels
   end.


The alloc/1 function doesn't care to check for the
case that the server runs out of free resources, but
neither does the calling function. The most common
situation would be to make the server return an error
code rather than crashing, of course, but all this
would most likely distract the reader from the actual
point of the example.

BR,
/Ulf W
-- 
Ulf Wiger




More information about the erlang-bugs mailing list