EMZETT.
Login

Kurz: > Achtung: Das Ende einer Funktion braucht einen Punkt, Klauseln trennt man mit ;, Ausdrücke mit ,. Verwechslungen davon sind der häufigste Anfängerfehler (syntax error before:).

Teil des Kurses Erlang

Kapitel 8 von 8 im Kurs Erlang. Mit Fortschritt, Quiz und Zertifikat auf der Lernseite.

Wichtige Module

ModulZweck
listsListenfunktionen
mapsMap-Funktionen
string, binary, reText, Binaries, reguläre Ausdrücke
io, io_libEin- und Ausgabe
file, filenameDateien
ets, dets, mnesiaTabellen und Datenbank
gen_server, supervisorOTP-Behaviours
timer, erlangZeit, Systemfunktionen
math, randMathematik, Zufall
main(_) ->
    io:format("~p~n", [erlang:system_info(otp_release) >= "20"]),
    io:format("~p~n", [is_pid(self())]),
    io:format("~p~n", [is_function(fun(X) -> X end)]),
    io:format("~p~n", [erlang:abs(-7)]),
    io:format("~p~n", [element(1, {a, b})]),
    io:format("~p~n", [is_list("abc")]),
    io:format("~p~n", [atom_to_list(hallo)]),
    io:format("~p~n", [list_to_atom("welt")]),
    io:format("~p~n", [length([])]),
    io:format("~p~n", [min(3, 9)]),
    io:format("~p~n", [rand:uniform(6) >= 1]),
    io:format("~p~n", [is_binary(<<1>>)]),
    io:format("~p~n", [erlang:process_info(self(), message_queue_len)]),
    io:format("~p~n", [timer:tc(fun() -> ok end) =/= error]),
    io:format("~p~n", [calendar:is_leap_year(2024)]),
    io:format("~p~n", [calendar:day_of_the_week(2024, 3, 15)]),
    io:format("~p~n", [filename:extension("a/b/c.txt")]),
    io:format("~p~n", [filename:join(["a", "b", "c.txt"])]),
    io:format("~p~n", [file:read_file("/nicht/da")]).

Ausgabe:

true
true
true
7
a
true
"hallo"
welt
0
3
true
true
{message_queue_len,0}
true
true
5
".txt"
"a/b/c.txt"
{error,enoent}

Typische Fehler

main(_) ->
    X = 1,
    X = 2.

Fehlermeldung:

no match of right hand side value 2
main(_) ->
    unbekannt:funktion().

Fehlermeldung:

undef

Achtung

Das Ende einer Funktion braucht einen Punkt, Klauseln trennt man mit ;, Ausdrücke mit ,. Verwechslungen davon sind der häufigste Anfängerfehler (syntax error before:).

Weiterführende Quellen

  • erlang.org/doc: Dokumentation und Reference Manual
  • „Learn You Some Erlang for Great Good!“ (learnyousomeerlang.com, kostenlos)
  • „Erlang and OTP in Action“ und „Designing for Scalability with Erlang/OTP“
  • adoptingerlang.org: Praxis mit rebar3 und Releases

Merke

  • Erlang: funktional, nebenläufig, fehlertolerant
  • Prozesse, Nachrichten und Supervisor sind das Kernrezept
  • BEAM, OTP und rebar3 bilden die Werkzeugkette

Übungsaufgabe

Baue einen kleinen Chat-Server mit einem Prozess pro Client.

Quiz zur Selbstkontrolle

Weiter im Kurs

Zurück: ETS, Fehler und Praxisbeispiele

Alle Kapitel: Erlang im Überblick