[erlang/en] Various (mostly cosmetic) improvements

* Add missing square brackets in example of pattern matching on lists
* Keep lines under 80 characters
* Hiphenate complex modifiers (e.g. "pattern-matching operation")
* Consistently proper case the language name
* Improve punctuation in comments
* Properly format Markdown inline code
* Tense changes (where appropriate)
This commit is contained in:
Julien Cretel 2015-05-18 11:55:43 +01:00
parent 48c24f7e45
commit c33063fe7a

View File

@ -18,7 +18,7 @@ filename: learnerlang.erl
% Periods (`.`) (followed by whitespace) separate entire functions and % Periods (`.`) (followed by whitespace) separate entire functions and
% expressions in the shell. % expressions in the shell.
% Semicolons (`;`) separate clauses. We find clauses in several contexts: % Semicolons (`;`) separate clauses. We find clauses in several contexts:
% function definitions and in `case`, `if`, `try..catch` and `receive` % function definitions and in `case`, `if`, `try..catch`, and `receive`
% expressions. % expressions.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -27,20 +27,20 @@ filename: learnerlang.erl
Num = 42. % All variable names must start with an uppercase letter. Num = 42. % All variable names must start with an uppercase letter.
% Erlang has single assignment variables, if you try to assign a different value % Erlang has single-assignment variables; if you try to assign a different
% to the variable `Num`, youll get an error. % value to the variable `Num`, youll get an error.
Num = 43. % ** exception error: no match of right hand side value 43 Num = 43. % ** exception error: no match of right hand side value 43
% In most languages, `=` denotes an assignment statement. In Erlang, however, % In most languages, `=` denotes an assignment statement. In Erlang, however,
% `=` denotes a pattern matching operation. `Lhs = Rhs` really means this: % `=` denotes a pattern-matching operation. `Lhs = Rhs` really means this:
% evaluate the right side (Rhs), and then match the result against the pattern % evaluate the right side (`Rhs`), and then match the result against the
% on the left side (Lhs). % pattern on the left side (`Lhs`).
Num = 7 * 6. Num = 7 * 6.
% Floating point number. % Floating-point number.
Pi = 3.14159. Pi = 3.14159.
% Atoms, are used to represent different non-numerical constant values. Atoms % Atoms are used to represent different non-numerical constant values. Atoms
% start with lowercase letters, followed by a sequence of alphanumeric % start with lowercase letters, followed by a sequence of alphanumeric
% characters or the underscore (`_`) or at (`@`) sign. % characters or the underscore (`_`) or at (`@`) sign.
Hello = hello. Hello = hello.
@ -53,34 +53,34 @@ AtomWithSpace = 'some atom with space'.
% Tuples are similar to structs in C. % Tuples are similar to structs in C.
Point = {point, 10, 45}. Point = {point, 10, 45}.
% If we want to extract some values from a tuple, we use the pattern matching % If we want to extract some values from a tuple, we use the pattern-matching
% operator `=`. % operator `=`.
{point, X, Y} = Point. % X = 10, Y = 45 {point, X, Y} = Point. % X = 10, Y = 45
% We can use `_` as a placeholder for variables that were not interested in. % We can use `_` as a placeholder for variables that were not interested in.
% The symbol `_` is called an anonymous variable. Unlike regular variables, % The symbol `_` is called an anonymous variable. Unlike regular variables,
% several occurrences of _ in the same pattern dont have to bind to the same % several occurrences of `_` in the same pattern dont have to bind to the
% value. % same value.
Person = {person, {name, {first, joe}, {last, armstrong}}, {footsize, 42}}. Person = {person, {name, {first, joe}, {last, armstrong}}, {footsize, 42}}.
{_, {_, {_, Who}, _}, _} = Person. % Who = joe {_, {_, {_, Who}, _}, _} = Person. % Who = joe
% We create a list by enclosing the list elements in square brackets and % We create a list by enclosing the list elements in square brackets and
% separating them with commas. % separating them with commas.
% The individual elements of a list can be of any type. % The individual elements of a list can be of any type.
% The first element of a list is the head of the list. If you imagine removing the % The first element of a list is the head of the list. If you imagine removing
% head from the list, whats left is called the tail of the list. % the head from the list, whats left is called the tail of the list.
ThingsToBuy = [{apples, 10}, {pears, 6}, {milk, 3}]. ThingsToBuy = [{apples, 10}, {pears, 6}, {milk, 3}].
% If `T` is a list, then `[H|T]` is also a list, with head `H` and tail `T`. % If `T` is a list, then `[H|T]` is also a list, with head `H` and tail `T`.
% The vertical bar (`|`) separates the head of a list from its tail. % The vertical bar (`|`) separates the head of a list from its tail.
% `[]` is the empty list. % `[]` is the empty list.
% We can extract elements from a list with a pattern matching operation. If we % We can extract elements from a list with a pattern-matching operation. If we
% have a nonempty list `L`, then the expression `[X|Y] = L`, where `X` and `Y` % have a nonempty list `L`, then the expression `[X|Y] = L`, where `X` and `Y`
% are unbound variables, will extract the head of the list into `X` and the tail % are unbound variables, will extract the head of the list into `X` and the tail
% of the list into `Y`. % of the list into `Y`.
[FirstThing|OtherThingsToBuy] = ThingsToBuy. [FirstThing|OtherThingsToBuy] = ThingsToBuy.
% FirstThing = {apples, 10} % FirstThing = {apples, 10}
% OtherThingsToBuy = {pears, 6}, {milk, 3} % OtherThingsToBuy = [{pears, 6}, {milk, 3}]
% There are no strings in Erlang. Strings are really just lists of integers. % There are no strings in Erlang. Strings are really just lists of integers.
% Strings are enclosed in double quotation marks (`"`). % Strings are enclosed in double quotation marks (`"`).
@ -117,17 +117,19 @@ c(geometry). % {ok,geometry}
geometry:area({rectangle, 10, 5}). % 50 geometry:area({rectangle, 10, 5}). % 50
geometry:area({circle, 1.4}). % 6.15752 geometry:area({circle, 1.4}). % 6.15752
% In Erlang, two functions with the same name and different arity (number of arguments) % In Erlang, two functions with the same name and different arity (number of
% in the same module represent entirely different functions. % arguments) in the same module represent entirely different functions.
-module(lib_misc). -module(lib_misc).
-export([sum/1]). % export function `sum` of arity 1 accepting one argument: list of integers. -export([sum/1]). % export function `sum` of arity 1
% accepting one argument: list of integers.
sum(L) -> sum(L, 0). sum(L) -> sum(L, 0).
sum([], N) -> N; sum([], N) -> N;
sum([H|T], N) -> sum(T, H+N). sum([H|T], N) -> sum(T, H+N).
% Funs are "anonymous" functions. They are called this way because they have no % Funs are "anonymous" functions. They are called this way because they have
% name. However they can be assigned to variables. % no name. However, they can be assigned to variables.
Double = fun(X) -> 2*X end. % `Double` points to an anonymous function with handle: #Fun<erl_eval.6.17052888> Double = fun(X) -> 2 * X end. % `Double` points to an anonymous function
% with handle: #Fun<erl_eval.6.17052888>
Double(2). % 4 Double(2). % 4
% Functions accept funs as their arguments and can return funs. % Functions accept funs as their arguments and can return funs.
@ -140,8 +142,9 @@ Triple(5). % 15
% The notation `[F(X) || X <- L]` means "the list of `F(X)` where `X` is taken % The notation `[F(X) || X <- L]` means "the list of `F(X)` where `X` is taken
% from the list `L`." % from the list `L`."
L = [1,2,3,4,5]. L = [1,2,3,4,5].
[2*X || X <- L]. % [2,4,6,8,10] [2 * X || X <- L]. % [2,4,6,8,10]
% A list comprehension can have generators and filters which select subset of the generated values. % A list comprehension can have generators and filters, which select subset of
% the generated values.
EvenNumbers = [N || N <- [1, 2, 3, 4], N rem 2 == 0]. % [2, 4] EvenNumbers = [N || N <- [1, 2, 3, 4], N rem 2 == 0]. % [2, 4]
% Guards are constructs that we can use to increase the power of pattern % Guards are constructs that we can use to increase the power of pattern
@ -155,15 +158,15 @@ max(X, Y) -> Y.
% A guard is a series of guard expressions, separated by commas (`,`). % A guard is a series of guard expressions, separated by commas (`,`).
% The guard `GuardExpr1, GuardExpr2, ..., GuardExprN` is true if all the guard % The guard `GuardExpr1, GuardExpr2, ..., GuardExprN` is true if all the guard
% expressions `GuardExpr1, GuardExpr2, ...` evaluate to true. % expressions `GuardExpr1`, `GuardExpr2`, ..., `GuardExprN` evaluate to `true`.
is_cat(A) when is_atom(A), A =:= cat -> true; is_cat(A) when is_atom(A), A =:= cat -> true;
is_cat(A) -> false. is_cat(A) -> false.
is_dog(A) when is_atom(A), A =:= dog -> true; is_dog(A) when is_atom(A), A =:= dog -> true;
is_dog(A) -> false. is_dog(A) -> false.
% A `guard sequence` is either a single guard or a series of guards, separated % A guard sequence is either a single guard or a series of guards, separated
%by semicolons (`;`). The guard sequence `G1; G2; ...; Gn` is true if at least % by semicolons (`;`). The guard sequence `G1; G2; ...; Gn` is true if at
% one of the guards `G1, G2, ...` evaluates to true. % least one of the guards `G1`, `G2`, ..., `Gn` evaluates to `true`.
is_pet(A) when is_dog(A); is_cat(A) -> true; is_pet(A) when is_dog(A); is_cat(A) -> true;
is_pet(A) -> false. is_pet(A) -> false.
@ -188,7 +191,7 @@ X = #todo{}.
X1 = #todo{status = urgent, text = "Fix errata in book"}. X1 = #todo{status = urgent, text = "Fix errata in book"}.
% #todo{status = urgent, who = joe, text = "Fix errata in book"} % #todo{status = urgent, who = joe, text = "Fix errata in book"}
X2 = X1#todo{status = done}. X2 = X1#todo{status = done}.
% #todo{status = done,who = joe,text = "Fix errata in book"} % #todo{status = done, who = joe, text = "Fix errata in book"}
% `case` expressions. % `case` expressions.
% `filter` returns a list of all elements `X` in a list `L` for which `P(X)` is % `filter` returns a list of all elements `X` in a list `L` for which `P(X)` is
@ -209,8 +212,8 @@ max(X, Y) ->
true -> nil true -> nil
end. end.
% Warning: at least one of the guards in the `if` expression must evaluate to true; % Warning: at least one of the guards in the `if` expression must evaluate to
% otherwise, an exception will be raised. % `true`; otherwise, an exception will be raised.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -218,7 +221,7 @@ max(X, Y) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Exceptions are raised by the system when internal errors are encountered or % Exceptions are raised by the system when internal errors are encountered or
% explicitly in code by calling `throw(Exception)`, `exit(Exception)` or % explicitly in code by calling `throw(Exception)`, `exit(Exception)`, or
% `erlang:error(Exception)`. % `erlang:error(Exception)`.
generate_exception(1) -> a; generate_exception(1) -> a;
generate_exception(2) -> throw(a); generate_exception(2) -> throw(a);
@ -227,7 +230,7 @@ generate_exception(4) -> {'EXIT', a};
generate_exception(5) -> erlang:error(a). generate_exception(5) -> erlang:error(a).
% Erlang has two methods of catching an exception. One is to enclose the call to % Erlang has two methods of catching an exception. One is to enclose the call to
% the function, which raised the exception within a `try...catch` expression. % the function that raises the exception within a `try...catch` expression.
catcher(N) -> catcher(N) ->
try generate_exception(N) of try generate_exception(N) of
Val -> {N, normal, Val} Val -> {N, normal, Val}
@ -241,23 +244,24 @@ catcher(N) ->
% exception, it is converted into a tuple that describes the error. % exception, it is converted into a tuple that describes the error.
catcher(N) -> catch generate_exception(N). catcher(N) -> catch generate_exception(N).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 4. Concurrency %% 4. Concurrency
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Erlang relies on the actor model for concurrency. All we need to write % Erlang relies on the actor model for concurrency. All we need to write
% concurrent programs in erlang are three primitives: spawning processes, % concurrent programs in Erlang are three primitives: spawning processes,
% sending messages and receiving messages. % sending messages and receiving messages.
% To start a new process we use the `spawn` function, which takes a function % To start a new process, we use the `spawn` function, which takes a function
% as argument. % as argument.
F = fun() -> 2 + 2 end. % #Fun<erl_eval.20.67289768> F = fun() -> 2 + 2 end. % #Fun<erl_eval.20.67289768>
spawn(F). % <0.44.0> spawn(F). % <0.44.0>
% `spawn` returns a pid (process identifier), you can use this pid to send % `spawn` returns a pid (process identifier); you can use this pid to send
% messages to the process. To do message passing we use the `!` operator. % messages to the process. To do message passing, we use the `!` operator.
% For all of this to be useful we need to be able to receive messages. This is % For all of this to be useful, we need to be able to receive messages. This is
% achieved with the `receive` mechanism: % achieved with the `receive` mechanism:
-module(calculateGeometry). -module(calculateGeometry).
@ -272,12 +276,13 @@ calculateArea() ->
io:format("We can only calculate area of rectangles or circles.") io:format("We can only calculate area of rectangles or circles.")
end. end.
% Compile the module and create a process that evaluates `calculateArea` in the shell % Compile the module and create a process that evaluates `calculateArea` in the
% shell.
c(calculateGeometry). c(calculateGeometry).
CalculateArea = spawn(calculateGeometry, calculateArea, []). CalculateArea = spawn(calculateGeometry, calculateArea, []).
CalculateArea ! {circle, 2}. % 12.56000000000000049738 CalculateArea ! {circle, 2}. % 12.56000000000000049738
% The shell is also a process, you can use `self` to get the current pid % The shell is also a process; you can use `self` to get the current pid.
self(). % <0.41.0> self(). % <0.41.0>
``` ```