Here's my code in 'factorial.erl':
-module(factorial). -author("jasonzhu"). %% API -export([fac/1]). fac(0) -> 1; fac(N) -> N * fac(N-1). When interacting this code in prompt, it works fine:
1> c(factorial). {ok,factorial} 2> factorial:fac(20). 2432902008176640000 But if I compile and execute it from command line, some errors occurred.
Jasons-MacBook-Pro:src jasonzhu$ erlc factorial.erl Jasons-MacBook-Pro:src jasonzhu$ erl -noshell -s factorial fac 20 -s init stop {"init terminating in do_boot",{badarith,[{factorial,fac,1,[{file,"factorial.erl"},{line,8}]},{init,start_it,1,[]},{init,start_em,1,[]}]}} Crash dump was written to: erl_crash.dump init terminating in do_boot () Could anyone help me out? I'm a newbie on Erlang, many thanks!
2 Answers
-noshell syntax is
erl -noshell -s Module Function Arguments where Arguments is a list of atoms. So you have to get 'fac' argument from list and convert it to integer.
This
-module(factorial). -export([fac/1]). fac([N]) -> X = fac(list_to_integer(atom_to_list(N))), io:format("~p~n", [X]); fac(0) -> 1; fac(N) -> N * fac(N-1). works
>>> erl -noshell -s factorial fac 20 -s init stop 2432902008176640000 This option is not specific to the OP's question just may be useful for someone coming from “init terminating in do_boot” search in addition to the official doc How to Interpret the Erlang Crash Dumps
If your code is laying around for some time and you start working with it again, recompilation may make this error gone
- Delete _build/ directory
- Delete rebar.lock file
- Compile
/dir_with_rebarconfig$ rebar3 release