summaryrefslogtreecommitdiffhomepage
path: root/erlang.html.markdown
diff options
context:
space:
mode:
authorKristin Linn <klinn@upenn.edu>2015-10-20 16:26:35 -0400
committerKristin Linn <klinn@upenn.edu>2015-10-20 16:26:35 -0400
commit396e6f5d9708f827512c4699240f72477366ff76 (patch)
treed63b41a4d91ea80c594574c48fc6416d6fd9a538 /erlang.html.markdown
parent11aab085d656b79482e92a05acbbac81125bfb78 (diff)
parent5fb5dd7c7fd7670faca6b8cfff9ef1ffdbd65c0d (diff)
Merge branch 'master' of https://github.com/adambard/learnxinyminutes-docs
Diffstat (limited to 'erlang.html.markdown')
-rw-r--r--erlang.html.markdown44
1 files changed, 40 insertions, 4 deletions
diff --git a/erlang.html.markdown b/erlang.html.markdown
index 8b67a76a..d6ed7b86 100644
--- a/erlang.html.markdown
+++ b/erlang.html.markdown
@@ -25,6 +25,7 @@ filename: learnerlang.erl
%% 1. Variables and pattern matching.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% In Erlang new variables are bound with an `=` statement.
Num = 42. % All variable names must start with an uppercase letter.
% Erlang has single-assignment variables; if you try to assign a different
@@ -32,9 +33,11 @@ Num = 42. % All variable names must start with an uppercase letter.
Num = 43. % ** exception error: no match of right hand side value 43
% In most languages, `=` denotes an assignment statement. In Erlang, however,
-% `=` denotes a pattern-matching operation. `Lhs = Rhs` really means this:
-% evaluate the right side (`Rhs`), and then match the result against the
-% pattern on the left side (`Lhs`).
+% `=` denotes a pattern-matching operation. When an empty variable is used on the
+% left hand side of the `=` operator to is bound (assigned), but when a bound
+% variable is used on the left hand side the following behaviour is observed.
+% `Lhs = Rhs` really means this: evaluate the right side (`Rhs`), and then
+% match the result against the pattern on the left side (`Lhs`).
Num = 7 * 6.
% Floating-point number.
@@ -289,7 +292,7 @@ calculateArea() ->
_ ->
io:format("We can only calculate area of rectangles or circles.")
end.
-
+
% Compile the module and create a process that evaluates `calculateArea` in the
% shell.
c(calculateGeometry).
@@ -299,6 +302,39 @@ CalculateArea ! {circle, 2}. % 12.56000000000000049738
% The shell is also a process; you can use `self` to get the current pid.
self(). % <0.41.0>
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% 5. Testing with EUnit
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+% Unit tests can be written using EUnits's test generators and assert macros
+-module(fib).
+-export([fib/1]).
+-include_lib("eunit/include/eunit.hrl").
+
+fib(0) -> 1;
+fib(1) -> 1;
+fib(N) when N > 1 -> fib(N-1) + fib(N-2).
+
+fib_test_() ->
+ [?_assert(fib(0) =:= 1),
+ ?_assert(fib(1) =:= 1),
+ ?_assert(fib(2) =:= 2),
+ ?_assert(fib(3) =:= 3),
+ ?_assert(fib(4) =:= 5),
+ ?_assert(fib(5) =:= 8),
+ ?_assertException(error, function_clause, fib(-1)),
+ ?_assert(fib(31) =:= 2178309)
+ ].
+
+% EUnit will automatically export to a test() function to allow running the tests
+% in the erlang shell
+fib:test()
+
+% The popular erlang build tool Rebar is also compatible with EUnit
+% ```
+% rebar eunit
+% ```
+
```
## References