Hello, World!
最近Erlangがブームみたいです。久々に触手が動きました。
Haskellの時は触手が動かなかったのに(何故か)Erlangはキタ。(久々に)自然に身を任せてみる。
この辺(1・2・3・4・5)を見ながら「Hello, World!」とファイル操作関係を確認。大体30分くらいでした。
% apt-get install erlang % less -N ./hello.erl 1 -module( hello ). 2 -export( [say_hello/0] ). 3 4 say_hello( ) -> 5 io:fwrite( "Hello, World!\n" ). % /usr/bin/erl Erlang (BEAM) emulator version 5.5.2 [source] [async-threads:0] [kernel-poll:false] Eshell V5.5.2 (abort with ^G) 1> c( "./hello.erl" ). {ok,hello} 2> hello:say_hello( ). Hello, World! ok % less -N ./myfile.erl 1 -module( myfile ). 2 -export( [read_file/1, write_file/0, copy_file/0, rename_file/0, delete_file/0, copy_line/2] ). 3 4 read_file( ReadFile ) -> 5 file:read_file( ReadFile ). 6 7 write_file( ) -> 8 file:write_file( "./a.txt", <<"Hello, World!\n">>), 9 io:format( "./a.txt(~p): ~p~n", 10 [filelib:is_file( "./a.txt" ), file:read_file( "./a.txt" )] ). 11 12 copy_file( ) -> 13 file:copy( "./a.txt", "./b.txt" ), 14 io:format( "./a.txt(~p): ~p~n", 15 [filelib:is_file( "./a.txt" ), file:read_file( "./a.txt" )] ), 16 io:format( "./b.txt(~p): ~p~n", 17 [filelib:is_file( "./b.txt" ), file:read_file( "./b.txt" )] ). 18 19 rename_file( ) -> 20 file:rename( "./b.txt", "./c.txt" ), 21 io:format( "./b.txt(~p): ~p~n", 22 [filelib:is_file( "./b.txt" ), file:read_file( "./b.txt" )] ), 23 io:format( "./c.txt(~p): ~p~n", 24 [filelib:is_file( "./c.txt" ), file:read_file( "./c.txt" )] ). 25 26 delete_file( ) -> 27 file:delete( "./c.txt" ), 28 io:format( "./c.txt(~p): ~p~n", 29 [filelib:is_file( "./c.txt" ), file:read_file( "./c.txt" )] ). 30 31 copy_line( ReadFile, WriteFile ) -> 32 {ok, In} = file:open( ReadFile, read ), 33 {ok, Out} = file:open( WriteFile, write ), 34 copy_line( In, Out, 1 ), 35 file:close( Out ), 36 file:close( In ). 37 38 copy_line( In, Out, LineNumber ) -> 39 case io:get_line( In , "" ) of 40 eof -> 41 ok; 42 Line -> 43 io:format( Out , "~b ~s", [LineNumber, Line] ), 44 copy_line( In , Out , LineNumber + 1 ) 45 end. % /usr/bin/erl Erlang (BEAM) emulator version 5.5.2 [source] [async-threads:0] [kernel-poll:false] Eshell V5.5.2 (abort with ^G) 1> c( "./myfile.erl" ). {ok,myfile} 2> myfile:write_file( ). ./a.txt(true): {ok,<<"Hello, World!\n">>} ok 3> myfile:copy_file( ). ./a.txt(true): {ok,<<"Hello, World!\n">>} ./b.txt(true): {ok,<<"Hello, World!\n">>} ok 4> myfile:rename_file( ). ./b.txt(false): {error,enoent} ./c.txt(true): {ok,<<"Hello, World!\n">>} ok 5> myfile:delete_file( ). ./c.txt(false): {error,enoent} ok 6> myfile:copy_line( "./a.txt", "./e.txt" ). ok 7> myfile:read_file( "./e.txt" ). {ok,<<"1 Hello, World!\n">>}
うは。コレは結構面白い。
構文が微妙に変態チックだけど、スキルの低い自分でも(まだ)理解できる範囲。ただ、「~」は許容しがたい。
時間が無いので今日はココまで。次はスレッド周りを確認したい。。。って、こんな事してる暇ないのに。ヤバイ。(w
日本語のErlang本が出れば(今だったら)ものすごく売れるカモね。