aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2024-02-09 21:02:57 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2024-02-09 21:02:57 +0100
commit293916c8f4dc109082ecd709e5e4c19a0847b0cd (patch)
treed0178f9434e0f01b7c5b12850eceee126bc8f946
downloaderlang-workshop-293916c8f4dc109082ecd709e5e4c19a0847b0cd.tar.gz
erlang-workshop-293916c8f4dc109082ecd709e5e4c19a0847b0cd.tar.bz2
erlang-workshop-293916c8f4dc109082ecd709e5e4c19a0847b0cd.tar.xz
erlang-workshop-293916c8f4dc109082ecd709e5e4c19a0847b0cd.zip
wip
-rw-r--r--.gitignore1
-rw-r--r--README.md2
-rw-r--r--erlang_ls.config0
-rw-r--r--hello.erl0
-rw-r--r--step-01/README.md10
-rw-r--r--step-02/README:md26
-rw-r--r--step-02/step02.erl5
-rw-r--r--step-03/README.md98
-rw-r--r--step-03/tut1.erl7
-rw-r--r--step-03/tut2.erl8
-rw-r--r--step-03/tut3.erl7
-rw-r--r--step-04/README.md0
-rw-r--r--step-04/tut17.erl31
13 files changed, 195 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..17278c0
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.beam
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..af32b6e
--- /dev/null
+++ b/README.md
@@ -0,0 +1,2 @@
+
+* https://www.erlang.org/downloads#prebuilt
diff --git a/erlang_ls.config b/erlang_ls.config
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erlang_ls.config
diff --git a/hello.erl b/hello.erl
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/hello.erl
diff --git a/step-01/README.md b/step-01/README.md
new file mode 100644
index 0000000..40d0188
--- /dev/null
+++ b/step-01/README.md
@@ -0,0 +1,10 @@
+Test your environment:
+
+```bash
+$ erl
+Erlang/OTP 25 [erts-13.2.2.5] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit:ns]
+
+Eshell V13.2.2.5 (abort with ^G)
+1> halt().
+$
+```
diff --git a/step-02/README:md b/step-02/README:md
new file mode 100644
index 0000000..4f531be
--- /dev/null
+++ b/step-02/README:md
@@ -0,0 +1,26 @@
+Basic syntax demonstration!
+
+```erlang
+-module(step02).
+-export([double/1]).
+
+double(X) ->
+ 2 * X.
+```
+
+* Statements are terminated with a full stop: ".".
+* `module` declares the name of the module. Should match file name by default.
+* `export` names functions to export out of this module. Will be usable by other modules. The `func/N` syntax means *the function `func`* with *N* arguments.
+* `double(X)` is a function declaration and implementation.
+
+Using this module looks like this:
+
+```
+1> c(step02).
+{ok,step02}
+2> step02:double(4).
+8
+```
+
+* The `c/1` function compiles a module. Notice that it returns ok and a module name.
+* To call the function, the module is explicitly named.
diff --git a/step-02/step02.erl b/step-02/step02.erl
new file mode 100644
index 0000000..fe4b968
--- /dev/null
+++ b/step-02/step02.erl
@@ -0,0 +1,5 @@
+-module(step02).
+-export([double/1]).
+
+double(X) ->
+ 2*X.
diff --git a/step-03/README.md b/step-03/README.md
new file mode 100644
index 0000000..cd2fd9b
--- /dev/null
+++ b/step-03/README.md
@@ -0,0 +1,98 @@
+# More syntax
+
+A function is identified by its name (`foo`) and the number of arguments it has. A function can be declared multiple times while pattern matching on it's arguments:
+
+```
+fac(1) ->
+ 1;
+fac(N) ->
+ N * fac(N - 1).
+```
+
+Notice the semi-colon between the declarations.
+
+Try out [tut1](./tut1.erl):
+
+```
+1> c(tut1).
+{ok,tut1}
+2> tut1:fac(1).
+1
+3> tut1:fac(10).
+3628800
+```
+
+# Atoms
+
+An atom is data type that starts with a lower-case letter. In the previous examples `ok` and `tut1` are atoms. They are kinda like unmodifyable strings, but are used as identifiers.
+
+[tut2](./tut2.erl) is another example of overloading, but with atoms:
+
+```
+1> c(tut2).
+{ok,tut2}
+2> tut2:convert(23, centimeter).
+58.42
+```
+
+# More pattern matching
+
+Tuples are dentonated as: `{a, b, c}`. They can be used when pattern matching as seen in [tut3](./tut3.erl).
+
+```
+1> c(tut3).
+{ok,tut3}
+2> tut3:convert_length({inch, 23}).
+{centimeter,58.42}
+```
+
+# Lists
+
+Lists are dentonated as: `[a, b, c]`, for example:
+
+```erlang
+Temps = [
+ {moscow, {c, -10}},
+ {cape_town, {f, 70}},
+ {stockholm, {c, -4}},
+ {paris, {f, 28}},
+ {london, {f, 36}}
+].
+```
+
+Lists are, as they commonly are in functional languages, linked lists and it is common to work on the head of the list:
+
+```
+1> [First | Rest] = Temps.
+[{moscow,{c,-10}},
+ {cape_town,{f,70}},
+ {stockholm,{c,-4}},
+ {paris,{f,28}},
+ {london,{f,36}}]
+2> First.
+{moscow,{c,-10}}
+3> Rest.
+[{cape_town,{f,70}},
+ {stockholm,{c,-4}},
+ {paris,{f,28}},
+ {london,{f,36}}]
+```
+
+```
+1> [First, Second | Rest] = Temps.
+[{moscow,{c,-10}},
+ {cape_town,{f,70}},
+ {stockholm,{c,-4}},
+ {paris,{f,28}},
+ {london,{f,36}}]
+2> First.
+{moscow,{c,-10}}
+3> Second.
+{cape_town,{f,70}}
+4> Rest.
+[{stockholm,{c,-4}},{paris,{f,28}},{london,{f,36}}]
+```
+
+# Maps
+
+Maps are dentonated as `#{foo => bar}`
diff --git a/step-03/tut1.erl b/step-03/tut1.erl
new file mode 100644
index 0000000..2d8c465
--- /dev/null
+++ b/step-03/tut1.erl
@@ -0,0 +1,7 @@
+-module(tut1).
+-export([fac/1]).
+
+fac(1) ->
+ 1;
+fac(N) ->
+ N * fac(N - 1).
diff --git a/step-03/tut2.erl b/step-03/tut2.erl
new file mode 100644
index 0000000..ecdd70a
--- /dev/null
+++ b/step-03/tut2.erl
@@ -0,0 +1,8 @@
+-module(tut2).
+-export([convert/2]).
+
+convert(M, inch) ->
+ M / 2.54;
+
+convert(N, centimeter) ->
+ N * 2.54.
diff --git a/step-03/tut3.erl b/step-03/tut3.erl
new file mode 100644
index 0000000..71a500e
--- /dev/null
+++ b/step-03/tut3.erl
@@ -0,0 +1,7 @@
+-module(tut3).
+-export([convert_length/1]).
+
+convert_length({centimeter, X}) ->
+ {inch, X / 2.54};
+convert_length({inch, Y}) ->
+ {centimeter, Y * 2.54}.
diff --git a/step-04/README.md b/step-04/README.md
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/step-04/README.md
diff --git a/step-04/tut17.erl b/step-04/tut17.erl
new file mode 100644
index 0000000..6808340
--- /dev/null
+++ b/step-04/tut17.erl
@@ -0,0 +1,31 @@
+-module(tut17).
+
+-export([start_ping/1, start_pong/0, ping/2, pong/0]).
+
+ping(0, Pong_Node) ->
+ {pong, Pong_Node} ! finished,
+ io:format("ping finished~n", []);
+
+ping(N, Pong_Node) ->
+ {pong, Pong_Node} ! {ping, self()},
+ receive
+ pong ->
+ io:format("Ping received pong~n", [])
+ end,
+ ping(N - 1, Pong_Node).
+
+pong() ->
+ receive
+ finished ->
+ io:format("Pong finished~n", []);
+ {ping, Ping_PID} ->
+ io:format("Pong received ping~n", []),
+ Ping_PID ! pong,
+ pong()
+ end.
+
+start_pong() ->
+ register(pong, spawn(tut17, pong, [])).
+
+start_ping(Pong_Node) ->
+ spawn(tut17, ping, [3, Pong_Node]).