一個例子
這是取自SPOJ (強烈推薦這個OnlineJudge,幾乎支援任何程式設計語言,google之)的第一道題目的答案。從标準輸入讀入N行整數,遇到42就退出。
Why 42 ?
the answer to life, the universe and everything!
-module(tested).
-export([main/0]).
main() ->
case io:get_line("") of
{error, Why} -> io:format(Why);
"42\n" -> void;
Data -> io:format("~s~n",[Data]), main()
end.
%% your module MUST be named "tested"
第1種方式,在erlang的shell中互動式編譯運作
max@max-gentoo ~/Study/GitLab/SPOJ $ erl
Erlang/OTP 17 [erts-6.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V6.1 (abort with ^G)
1> c(tested).
{ok,tested}
2> tested:main()
2>
第2種方式,在指令行的提示符中運作
erl -noshell -s tested main -s init stop
第3種方式,碉炸天的erlang一行(沒有perl一行易用)
erl -eval 'io:format("Memory:~p~n",[erlang:memory(total)]).' -noshell -s init stop
第4種方式,escript腳本
#!/usr/bin/escript
main(_) ->
io:format("Hello World~n").