[erlang-questions] and the argument is...?

Håkan Stenholm hokan.stenholm@REDACTED
Sun May 10 10:21:20 CEST 2015


On 2015-05-10 08:24, Peter J Etheridge wrote:
> -module(fac/1). % from p.165 of joe's book
> -export([main/1]).
>  main([A])->
>  I = list_to_integer(atom_to_list(A)),
>  F = fac(I),
>  io:format("factorial ~w = ~w~n", [I,F]),
>  init:stop().
>
>  fac(0) -> 1;
>  fac(N) -> N*fac(N-1).
Replace
-module(fac/1).
with
-module(fac).

I ran the code via:

erl -s fac main ...

on the command line and it appears to work fine. But I have to say that 
it is probably simpler to compile using erlc on the command line and run 
the resulting .beam file interactively in the erlang shell (see the erl 
command) as you can then simplify the code into:

-module(fac).
-export([fac/1]).

  fac(0) -> 1;
  fac(N) -> N*fac(N-1).

This allow you to call fac:fac(...) directly, multiple times, with 
different input as well as many other things ...

The erlang shell is also much more convenient if you need to pass more 
complex arguments or if you want to pass the result of previously 
executed commands as input.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150510/befb99ef/attachment.htm>


More information about the erlang-questions mailing list