Kurz: main(_) -> try teile(1, 0) of Wert -> io:format(“Wert pn”, [Wert]) catch error:badarith -> io:format(“durch null~n”) end, try throw(mein_wurf) catch throw:Was -> io:format(“geworfen.
Teil des Kurses Erlang
Ausnahmen
teile(A, B) -> A / B.
main(_) ->
try teile(1, 0) of
Wert -> io:format("Wert ~p~n", [Wert])
catch
error:badarith -> io:format("durch null~n")
end,
try throw(mein_wurf)
catch throw:Was -> io:format("geworfen: ~p~n", [Was])
end,
try exit(beenden)
catch exit:Grund -> io:format("exit: ~p~n", [Grund])
end,
try error({eigener_fehler, 42})
catch error:{eigener_fehler, Code} -> io:format("Code ~p~n", [Code])
after io:format("after läuft immer~n")
end,
Ergebnis = (catch teile(4, 2)),
io:format("~p~n", [Ergebnis]),
try list_to_integer("abc")
catch error:badarg -> io:format("keine Zahl~n")
end,
try lists:nth(5, [1, 2])
catch error:function_clause -> io:format("zu kurz~n")
end.Ausgabe:
durch null
geworfen: mein_wurf
exit: beenden
Code 42
after l�uft immer
2.0
keine Zahl
zu kurzKonvention {ok, Wert} / {error, Grund}
teilen(_, 0) -> {error, durch_null};
teilen(A, B) -> {ok, A div B}.
lies_zahl(Text) ->
try {ok, list_to_integer(Text)}
catch error:badarg -> {error, keine_zahl}
end.
rechne(A, B) ->
case lies_zahl(A) of
{ok, X} ->
case lies_zahl(B) of
{ok, Y} -> teilen(X, Y);
Fehler -> Fehler
end;
Fehler -> Fehler
end.
main(_) ->
io:format("~p~n", [rechne("20", "4")]),
io:format("~p~n", [rechne("20", "0")]),
io:format("~p~n", [rechne("x", "4")]),
io:format("~p~n", [rechne("20", "y")]).Ausgabe:
{ok,5}
{error,durch_null}
{error,keine_zahl}
{error,keine_zahl}ETS: Tabellen im Speicher
ETS-Tabellen sind schnelle, im Arbeitsspeicher gehaltene Schlüssel-Wert-Tabellen:
main(_) ->
T = ets:new(tabelle, [set, public]),
ets:insert(T, {mia, 17}),
ets:insert(T, {tom, 25}),
ets:insert(T, [{zoe, 31}, {ben, 12}]),
io:format("~p~n", [ets:lookup(T, mia)]),
io:format("~p~n", [ets:lookup(T, nobody)]),
io:format("~p~n", [ets:info(T, size)]),
ets:insert(T, {mia, 18}),
io:format("~p~n", [ets:lookup_element(T, mia, 2)]),
ets:update_counter(T, tom, 5),
io:format("~p~n", [ets:lookup(T, tom)]),
io:format("~p~n", [lists:sort(ets:tab2list(T))]),
io:format("~p~n", [lists:sort(ets:match(T, {'$1', '$2'}))]),
io:format("~p~n", [ets:select(T, [{{'$1', '$2'}, [{'>', '$2', 20}], ['$1']}]) -- [] =/= []]),
ets:delete(T, ben),
io:format("~p~n", [ets:info(T, size)]),
ets:delete(T).Ausgabe:
[{mia,17}]
[]
4
18
[{tom,30}]
[{ben,12},{mia,18},{tom,30},{zoe,31}]
[[ben,12],[mia,18],[tom,30],[zoe,31]]
true
3Praxis: Primzahlen parallel
ist_prim(N) when N < 2 -> false;
ist_prim(2) -> true;
ist_prim(N) -> not lists:any(fun(I) -> N rem I =:= 0 end, lists:seq(2, trunc(math:sqrt(N)))).
parallel_map(F, Liste) ->
Eltern = self(),
Pids = [spawn(fun() -> Eltern ! {self(), F(X)} end) || X <- Liste],
[receive {Pid, R} -> R end || Pid <- Pids].
main(_) ->
Zahlen = lists:seq(1, 50),
Flags = parallel_map(fun ist_prim/1, Zahlen),
Primzahlen = [Z || {Z, true} <- lists:zip(Zahlen, Flags)],
io:format("~p~n", [Primzahlen]),
Grosse = parallel_map(fun(N) -> length([X || X <- lists:seq(1, N), N rem X =:= 0]) end, [100, 1000, 10000]),
io:format("Teilerzahlen: ~p~n", [Grosse]),
io:format("~p~n", [length(parallel_map(fun(X) -> X * X end, lists:seq(1, 1000)))]).Ausgabe:
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]
Teilerzahlen: [9,16,25]
1000Merke
try ... catch Klasse:Grund ... afterfängterror,throwundexitab- Konvention
{ok, Wert}/{error, Grund}für erwartbare Fehler - ETS speichert Daten schnell im Speicher:
ets:new,insert,lookup,select - Prozesse parallelisieren Berechnungen einfach per
spawnund Nachrichten
Übungsaufgabe
Schreibe parallel_map mit Timeout und teste es mit einer langsamen Funktion.
Quiz zur Selbstkontrolle
Welche drei Fehlerklassen kennt Erlang?
- error, throw, exit (richtig)
- warning, error, fatal
- syntax, runtime, logic
- try, catch, finally
Wofür ist ETS?
- Schnelle Tabellen im Arbeitsspeicher (richtig)
- Ein Netzwerkprotokoll
- Ein Dateiformat
- Ein Testframework
Was bedeutet {ok, Wert}?
- Eine Erfolgsmeldung mit Ergebnis (richtig)
- Ein Fehler
- Ein Atom
- Ein String
Weiter im Kurs
Zurück: OTP: gen_server und Supervisor
Weiter: Referenz und Spickzettel
Alle Kapitel: Erlang im Überblick