いろいろErlang

Erlang勉強中。

Getting Startedな文書
http://www.erlang.org/doc/getting_started/part_frame.html

Hello World

以下はhelloworld.erlというファイルで保存する必要がある。

-module(helloworld).
-export([helloworld/0])

% これはHelloWorldだよ
helloworld() ->
 io:format("Hello", []),
 io:format(" World~n", []).

実行するには、以下のようにコマンドプロンプトで実行すればよい:

$> erl
$> c(helloworld).
$> helloworld:helloworld().
リストとタプル

リストは以下のように書く。ネストできる。

[a, b, c, d, [nested_a, nested_b]]

タプルは以下のように書く。ネストできる。

{a, b, c, d, {nested_a, nested_b}}

High Order Functions

JavaScriptの関数オブジェクトと似たような感じ。

(func(Hoge) -> Hoge + 3 end)(4). % 7 を返す。

list:map((func(Hoge) -> 3 * Hoge end), [1, 2, 3]). % [3, 6, 9] を返す。

Atom

Erlangにはatomというデータ型(?)があります。プログラム中では以下のように記述します。

atom

英語の小文字から始まっていることが条件です。任意の文字をatomにするには:

'Arbitary String'

とシングルクォーテーションを使えば任意も文字をatomにすることができます。
Oracle SQLにおけるダブルクォーテーションで識別子を指定するのに似ています。)