Erlang をはじめよう その3
前回 - mog project: Getting Started with Erlang pt.2 の続き
CHAPTER 5: Communicating with Humans
文字列リテラル
Unicode も標準で扱うことができる。
> io:format("h\"e\'l'l\\o\s\127\x4f\trld\n").
> io:format("~p ~p ~p ~w ~s ~c ~tc ~i ~n", [100, true, "yes", "yes", "yes", 88, 16#3042, 200]).
> $1.
> $A.
> $ .
> $あ.
文字列の操作
各種標準関数など。
> "erl" ++ "ang" == "erlang"
> string:concat("erl", "ang") =:= "erlang".
> hd("hello").
> hd('hello'). % error (this is an atom)
> length("hello").
> lists:nth(2, "hello").
> io:format("~c~n", [lists:nth(2, "hello")]).
> string:chr("hello", $l).
> string:str("hello", "lo").
> string:substr("hello", 3).
> string:substr("hello", 3, 2).
> string:sub_string("hello", 3, 4).
> string:tokens("this is a token.", " .").
> string:join(["one", "two", "three"], ",").
> string:words("this is a word.").
> string:chars($*, 10).
> string:copies("* ", 10).
> string:strip(" x \n").
> string:strip(" x ").
> string:left("x", 10).
> string:right("x", 10).
> string:centre("x", 10).
> lists:reverse("erlang").
> string:to_float("1.1 2 3").
> string:to_float("1 2 3").
> string:to_integer("1 2 3").
> string:to_integer("a1 2 3").
> string:to_lower("Hello World!").
> string:to_upper("Hello World!").
> integer_to_list(123).
> float_to_list(123.45).
> erlang:fun_to_list(fun(X) -> X * 2 end).
> list_to_atom("Hello").
ユーザ入力の読み取り
> io:read(">>> ").
>>> [1,2,3].
> io:read(">>> ").
>>> True. % error
> io:get_chars(">>> ").
>>> 1
> io:get_line(">>> ").
>>> 1 2 3
CHAPTER 6: Lists
基本的な操作
> [1,X,4,Y] = [1,2,4,8].
> {X, Y}.
> lists:flatten([1,[2,4,8],16]).
> [1,2,4] ++ [8,16].
> lists:append([1,2,4], [8,16]).
> lists:append([[1,2,4], [8,16], [32,64]]).
> lists:seq(1, 10).
> lists:seq($A, $Z).
head と tail の操作
> [H1 | T1] = [1, 2, 4].
> {H1, T1}.
> [H2 | T2] = [1].
> {H2, T2}.
> [H3 | T3] = [].
> F = (fun ([], _) -> 1; ([Head|Tail], Fun) -> Head * Fun(Tail, Fun) end).
> F([1,2,4,16], F).
> [1|[2,3]].
> [1,2|[3]].
> [1,2|3].
> [[1,2]|[3]].
zip と key-value 操作
> T = lists:zip([1,2,3,4,5], [a,b,c,d,e]).
> lists:unzip(T).
> lists:keystore(7,1,[{1,tiger}, {3,bear}],{7,panther}).
> lists:keyreplace(7,1,[{1,tiger}, {3,bear}],{7,panther}).
> lists:keyfind(3,1,[{1,tiger}, {3,bear}, {7,panther}]).
> lists:keyfind(4,1,[{1,tiger}, {3,bear}, {7,panther}]).
CHAPTER 7:
Higher-Order Functions and List Comprehensions
単純な高階関数
> Tripler = fun (Value, Function) -> 3 * Function(Value) end. > Tripler(6, fun(X)->20*X end). > X=20. > F=fun(Value)->X * Value end. > f(X). > X. % 'X' is unbound > Tripler(6, F). > Tripler(math:pi(), fun math:cos/1).
高階関数を使ってリストを操作する
> Print = fun(Value) -> io:format(" ~p~n", [Value]) end.
> List = [1,1,2,3,5,8,13].
> lists:foreach(Print, List).
> lists:map(fun(Value) -> Value * Value end, List).
> [Value * Value || Value lists:filter(fun(Value) -> (Value >= 3) and (Value rem 2 == 1) end, List).
> lists:filter(fun(Value) -> (Value >= 3) and (Value rem 2 == 1) end, List).
> [Value || Value = 3, Value rem 2 == 1].
> [Value || Value lists:all(fun(Value) -> Value > 0 end, List).
> lists:any(fun(Value) -> Value < 0 end, List).
> lists:partition(fun(Value) -> (Value >= 3) and (Value rem 2 == 1) end, List).
> lists:dropwhile(fun(Value) -> Value =< 3 end, List).
> lists:takewhile(fun(Value) -> Value =< 3 end, List).
> lists:foldl(fun(Value, Accumulator) -> Value - Accumulator end, 0, [1,2,3,4]).
> 4-(3-(2-(1-0))).
> lists:foldr(fun(Value, Accumulator) -> Value - Accumulator end, 0, [1,2,3,4]).
> 1-(2-(3-(4-0))).
References
0 件のコメント:
コメントを投稿