Skip to content

Instantly share code, notes, and snippets.

@pazworld
Last active January 5, 2023 07:39
Show Gist options
  • Save pazworld/2caf946cdabccd1140a0054d0eac3d02 to your computer and use it in GitHub Desktop.
Save pazworld/2caf946cdabccd1140a0054d0eac3d02 to your computer and use it in GitHub Desktop.
study "Learn You Some Erlang for Great Good!"

This is gist to study "Learn You Some Erlang for Great Good!"

This gist is merged into pazworld/study repository.

-module(calc).
-export([rpn/1, rpn_test/0]).
rpn(L) when is_list(L) ->
[Res] = lists:foldl(fun rpn/2, [], string:tokens(L, " ")),
Res.
rpn("+", [N1, N2 | S]) -> [N2 + N1 | S];
rpn("-", [N1, N2 | S]) -> [N2 - N1 | S];
rpn("*", [N1, N2 | S]) -> [N2 * N1 | S];
rpn("/", [N1, N2 | S]) -> [N2 / N1 | S];
rpn("^", [N1, N2 | S]) -> [math:pow(N2, N1) | S];
rpn("ln", [N | S]) -> [math:log(N) | S];
rpn("log10", [N | S]) -> [math:log10(N) | S];
rpn("sum", Stack) -> [lists:sum(Stack)];
rpn("prod", Stack) -> [lists:foldl(fun erlang:'*'/2, 1, Stack)];
rpn(X, Stack) -> [read(X) | Stack].
read(N) ->
case string:to_float(N) of
{error, no_float} -> list_to_integer(N);
{F, _} -> F
end.
rpn_test() ->
5 = rpn("2 3 +"),
87 = rpn("90 3 -"),
-4 = rpn("10 4 3 + 2 * -"),
-2.0 = rpn("10 4 3 + 2 * - 2 /"),
ok = try
rpn("90 34 12 33 55 66 + * - +")
catch
error:{badmatch,[_|_]} -> ok
end,
4037 = rpn("90 34 12 33 55 66 + * - + -"),
8.0 = rpn("2 3 ^"),
true = math:log(2.7) == rpn("2.7 ln"),
true = math:log10(2.7) == rpn("2.7 log10"),
50 = rpn("10 10 10 20 sum"),
10.0 = rpn("10 10 10 20 sum 5 /"),
1000.0 = rpn("10 10 20 0.5 prod"),
ok.
-module(dolphins).
-compile(export_all).
dolphin1() ->
receive
do_a_flip ->
io:format("How about no? ~n");
fish ->
io:format("So long and thanks for all the fish! ~n");
_ ->
io:format("Heh, we're smarter than you humans. ~n")
end.
dolphin2() ->
receive
{From, do_a_flip} ->
From ! "How about no?";
{From, fish} ->
From ! "So long and thanks for all the fish!";
_ ->
io:format("Heh, we're smarter than you humans. ~n")
end.
dolphin3() ->
receive
{From, do_a_flip} ->
From ! "How about no?",
dolphin3();
{From, fish} ->
From ! "So long and thanks for all the fish!";
_ ->
io:format("Heh, we're smarter than you humans. ~n"),
dolphin3()
end.
-module(kitchen).
-compile(export_all).
fridge1() ->
receive
{From, {store, _Food}} ->
From ! {self(), ok},
fridge1();
{From,{take, _Food}} ->
From ! {self(), not_found},
fridge1();
terminate ->
ok
end.
fridge2(FoodList) ->
receive
{From, {store, Food}} ->
From ! {self(), ok},
fridge2([Food|FoodList]);
{From, {take, Food}} ->
case lists:member(Food, FoodList) of
true ->
From ! {self(), {ok, Food}},
fridge2(lists:delete(Food, FoodList));
false ->
From ! {self(), not_found},
fridge2(FoodList)
end;
terminate ->
ok
end.
start(FoodList) ->
spawn(?MODULE, fridge2, [FoodList]).
store(Pid, Food) ->
Pid ! {self(), {store, Food}},
receive
{Pid, Msg} -> Msg
end.
take(Pid, Food) ->
Pid ! {self(), {take, Food}},
receive
{Pid, Msg} -> Msg
end.
store2(Pid, Food) ->
Pid ! {self(), {store, Food}},
receive
{Pid, Msg} -> Msg
after 3000 ->
timeout
end.
take2(Pid, Food) ->
Pid ! {self(), {take, Food}},
receive
{Pid, Msg} -> Msg
after 3000 ->
timeout
end.
-module(linkmon).
-compile(export_all).
myproc() ->
timer:sleep(5000),
exit(reason).
chain(0) ->
receive
_ -> ok
after 2000 ->
exit("chain dies here")
end;
chain(N) ->
Pid = spawn(fun() -> chain(N - 1) end),
link(Pid),
receive
_ -> ok
end.
start_critic() ->
spawn(?MODULE, critic, []).
judge(Pid, Band, Album) ->
Pid ! {self(), {Band, Album}},
receive
{Pid, Criticism} -> Criticism
after 2000 ->
timeout
end.
critic() ->
receive
{From, {"Range Against the Turing Machine", "Unit Testify"}} ->
From ! {self(), "They are great!"};
{From, {"System of a Downtime", "Memorize"}} ->
From ! {self(), "They're no Johnny Crash but they're good."};
{From, {"Johnny Crash", "The Token Ring of Fire"}} ->
From ! {self(), "Simply incredible."};
{From, {_Band, _Album}} ->
From ! {self(), "They are terrible!"}
end,
critic().
start_critic2() ->
spawn(?MODULE, restarter, []).
restarter() ->
process_flag(trap_exit, true),
Pid = spawn_link(?MODULE, critic, []),
register(critic, Pid),
receive
{'EXIT', Pid, normal} -> % not a crash
ok;
{'EXIT', Pid, shutdown} -> % manual termination, not a crash
ok;
{'EXIT', Pid, _} ->
restarter()
end.
judge2(Band, Album) ->
critic ! {self(), {Band, Album}},
Pid = whereis(critic),
receive
{Pid, Criticism} -> Criticism
after 2000 ->
timeout
end.
-module(multiproc).
-compile([export_all]).
sleep(T) ->
receive
after T -> ok
end.
flush() ->
receive
_ -> flush()
after 0 ->
ok
end.
important() ->
receive
{Priority, Message} when Priority > 10 ->
[Message | important()]
after 0 ->
normal()
end.
normal() ->
receive
{_, Message} ->
[Message | normal()]
after 0 ->
[]
end.
-module(road).
-compile(export_all).
main() ->
File = "road.txt",
{ok, Bin} = file:read_file(File),
Map = parse_map(Bin),
{{SumA, ListA}, {SumB, ListB}} = lists:foldl(fun go_map/2, {{0, []}, {0,[]}}, lists:reverse(Map)),
if SumA =< SumB -> ListA
; SumA > SumB -> ListB
end.
parse_map(Bin) when is_binary(Bin) ->
parse_map(binary_to_list(Bin));
parse_map(Bin) when is_list(Bin) ->
Values = [list_to_integer(X) || X <- string:tokens(Bin, "\r\n\t")],
group_vals(Values, []).
group_vals([], Acc) ->
lists:reverse(Acc);
group_vals([A,B,X|Rest], Acc) ->
group_vals(Rest, [{A,B,X}|Acc]).
go_map({A, B, X}, {{SumA, ListA}, {SumB, ListB}}) ->
NewA = if SumA =< SumB + X -> {SumA + A, [{a, A} | ListA]}
; SumA > SumB + X -> {SumB + X + A, [{a, A}, {x, X} | ListB]} end,
NewB = if SumB =< SumA + X -> {SumB + B, [{b, B} | ListB]}
; SumB > SumA + X -> {SumA + X + B, [{b, B}, {x, X} | ListA]} end,
{NewA, NewB}.
test() ->
{{10, [{a, 10}]}, {15, [{b, 15}]}} = go_map({10, 15, 0}, {{0, []}, {0, []}}),
{{15, [{a, 5}, {a, 10}]}, {14, [{b, 1}, {x, 3}, {a, 10}]}} = go_map({5, 1, 3}, {{10, [{a, 10}]}, {15, [{b, 15}]}}).
50
10
30
5
90
20
40
2
25
10
8
0
-module(useless).
-export([add/2, hello/0, greet_and_add_two/1]).
add(A, B) ->
A + B.
%% Shows greetings.
hello() ->
io:format("Hello, world!~n").
greet_and_add_two(X) ->
hello(),
add(X, 2).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment