Erlang
★Erlangシェル erlコマンド
$ erl
Erlang R14B04 (erts-5.8.5) [source] [64-bit] [rq:1] [async-threads:0] [kernel-poll:false]
Eshell V5.8.5 (abort with ^G)
1> help().
** shell internal commands **
b() -- display all variable bindings
e(N) -- repeat the expression in query <N>
f() -- forget all variable bindings
f(X) -- forget the binding of variable X
h() -- history
history(N) -- set how many previous commands to keep
results(N) -- set how many previous command results to keep
catch_exception(B) -- how exceptions are handled
v(N) -- use the value of query <N>
rd(R,D) -- define a record
rf() -- remove all record information
rf(R) -- remove record information about R
rl() -- display all record information
rl(R) -- display record information about R
rp(Term) -- display Term using the shell's record information
rr(File) -- read record information from File (wildcards allowed)
rr(F,R) -- read selected record information from file(s)
rr(F,R,O) -- read selected record information with options
** commands in module c **
bt(Pid) -- stack backtrace for a process
c(File) -- compile and load code in <File>
cd(Dir) -- change working directory
flush() -- flush any messages sent to the shell
help() -- help info
i() -- information about the system
ni() -- information about the networked system
i(X,Y,Z) -- information about pid <X,Y,Z>
l(Module) -- load or reload module
lc([File]) -- compile a list of Erlang modules
ls() -- list files in the current directory
ls(Dir) -- list files in directory <Dir>
m() -- which modules are loaded
m(Mod) -- information about module <Mod>
memory() -- memory allocation information
memory(T) -- memory allocation information of type <T>
nc(File) -- compile and load code in <File> on all nodes
nl(Module) -- load module on all nodes
pid(X,Y,Z) -- convert X,Y,Z to a Pid
pwd() -- print working directory
q() -- quit - shorthand for init:stop()
regs() -- information about registered processes
nregs() -- information about all registered processes
xm(M) -- cross reference check a module
y(File) -- generate a Yecc parser
** commands in module i (interpreter interface) **
ih() -- print help for the i module
true
・コードのロードパス
> code:get_path().
・ロードされた全モジュールの一覧
> code:all_loaded().
・ホームディレクトリ
> init:get_argument(home).
★基本
test.erl モジュール名.erl
-module(test).
-export([start/0]).
start() -> "Test".
モジュール名 Javaのクラス名に相当
ファンクション名 Javaのメソッド名に相当
-export([function_name/N]) N:引数の数
> c(test). コンパイル:erlファイル ⇒ beamファイル
> test:start(). 実行
変数・引数:大文字で始まる
定数(Atom)・メソッド名・モジュール名:小文字で始まる
★Tuple
>Name = {name, andy}.
{name,andy}
>Person = {Name, {age, 28}}.
{{name,andy},{age,28}}
>{{name, A}, {age, B}} = Person.
{{name,andy},{age,28}}
>A.
andy
>B.
28
>element(3,{aaa,bbb,ccc,ddd}). 要素のインデックスは1から始まる
ccc
>setelement(3,{aaa,bbb,ccc,ddd},eee).
{aaa,bbb,eee,ddd}
利用例
case Result of % ResultはTuple型
{ok, Message} -> save(Message);
{ng, ErrorMessage} -> log(ErrorMessage)
end.
★List
>Person = [{name, andy}, {age, 28}, {address, tokyo}].
[{name,andy},{age,28},{address,tokyo}]
>[Info1|Info2] = Person.
[{name,andy},{age,28},{address,tokyo}]
>Info1.
{name,andy}
>Info2.
[{age,28},{address,tokyo}]
>NewPerson = [{tall, 180}|Person].
[{tall,180},{name,andy},{age,28},{address,tokyo}]
>[72,101,108,108,111]. ASCII 文字の値のリスト
"Hello"
>[$H,$e,$l,$l,$o]. $Character表記
"Hello"
>lists:sort([4,5,3,2,6,1]).
[1,2,3,4,5,6]
>[1|[2,3]].
[1,2,3]
>[[1,2]|[2,3]]. ネストされたリスト
[[1,2],2,3]
>[1,2] ++ [3,4].
[1,2,3,4]
★Function
書き方1
outer(Number) . ->
case Number of
0 -> 1;
_ -> % Javaでswitchのdefaultに相当
Temp = 10 * Number,
Temp + 1
end.
書き方2
outer(0) . -> 1;
outer(Number) . ->
Temp = 10 * Number,
Temp + 1. % returnが不要
動的に呼び出す
apply( outer, [ 5 ])
true = test() Javaで単体テストassertXXXに相当
匿名Function
outer(Number) . ->
Inner = fun(A, B) -> A + B + Number end,
Inner(2, 3).
outer(Number) . ->
Inner = fun(0) -> 0;
(A) -> A * 10 + Number end,
Inner(3).
Functionは変数として使える
Inner = fun outer / 1
Inner = list_to_atom("outer")
Inner = list_to_existing_atom("outer")
outerが定義済の場合、Inner = outer Function
outerが未定義の場合、Inner = outer Atom