From 2e05ec8d955b62fef844bd460f5bb455e351d1d0 Mon Sep 17 00:00:00 2001 From: edholland Date: Fri, 2 Oct 2015 02:26:04 +0200 Subject: Add clarification on bind / match with = op in erlang. Fixes #1139 --- erlang.html.markdown | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'erlang.html.markdown') diff --git a/erlang.html.markdown b/erlang.html.markdown index 8b67a76a..d0af7f05 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 +% varaible 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. -- cgit v1.2.3 From 61597e603f78dc0645517a13b0b258236e5a3563 Mon Sep 17 00:00:00 2001 From: Ed Holland Date: Fri, 2 Oct 2015 01:11:10 +0100 Subject: Add section on eunit in erlang doc --- erlang.html.markdown | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'erlang.html.markdown') diff --git a/erlang.html.markdown b/erlang.html.markdown index 8b67a76a..31cdcdab 100644 --- a/erlang.html.markdown +++ b/erlang.html.markdown @@ -299,6 +299,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() fucntion to allo running the tests +% in the erlang shell +fib:test() + +% The popular erlang build tool Rebar is also compatible with EUnit +% ``` +% rebar eunit +% ``` + ``` ## References -- cgit v1.2.3 From f7cfb3b194ff4fb590173cb7e790c4effc1656c0 Mon Sep 17 00:00:00 2001 From: edholland Date: Sun, 4 Oct 2015 13:18:06 +0200 Subject: Fix poor formatting and typos --- erlang.html.markdown | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'erlang.html.markdown') diff --git a/erlang.html.markdown b/erlang.html.markdown index 64330867..48cee6ec 100644 --- a/erlang.html.markdown +++ b/erlang.html.markdown @@ -308,25 +308,25 @@ self(). % <0.41.0> % 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() fucntion to allo running the tests +-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() -- cgit v1.2.3 From 001a99b13a6dad47937d475e5b960728d9baf9a6 Mon Sep 17 00:00:00 2001 From: Tim Heaney Date: Mon, 5 Oct 2015 21:18:50 -0400 Subject: Typo: should be variable, not varaible --- erlang.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'erlang.html.markdown') diff --git a/erlang.html.markdown b/erlang.html.markdown index 48cee6ec..4e2f1d84 100644 --- a/erlang.html.markdown +++ b/erlang.html.markdown @@ -35,7 +35,7 @@ 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. When an empty variable is used on the % left hand side of the `=` operator to is bound (assigned), but when a bound -% varaible is used on the left hand side the following behaviour is observed. +% 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. -- cgit v1.2.3