aboutsummaryrefslogtreecommitdiff
path: root/step-03
diff options
context:
space:
mode:
Diffstat (limited to 'step-03')
-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
4 files changed, 120 insertions, 0 deletions
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}.