Erlang Programming/How To Do It Example

-module(tut).

  -export([rev/1,dob/1]).
  %% double
  dob([ S | T ]) ->
      F = [ 2*S | [] ],
      dob(T, F).
  dob([ S | T ], F) ->
      X = [ 2*S | F ],
      dob(T, X);
  dob([], X) ->
      rev(X).
  %% reverse
  rev([ S | T ]) ->
      F = [ S | [] ],
      demo(T, F).
  demo([ S | T ], C) ->
      F = [ S | C ],
      demo(T, F);
  demo([], L) ->
      [],
      L.

please note the scope of local variables in the above code

run :

  c(tut).
  tut:dob([1, 2, 3]).
  tut:rev(tut:dob([1, 2, 3])).

another module which is call quicksort

  -module(quicksort).
  -export([sort/1]).
  sort([ S | F ]) ->
      % for X < S, bind X to F
      sort([ X || X <- F, X < S ])
      ++ [S]
      ++ sort([ X || X <- F, X > S ]);
  sort([]) ->
      [].

run :

  c(quicksort).
  quicksort:sort([9,4,3,7,8,5,1,2,6]).