Mercurial > repos > other > SevenLanguagesInSevenWeeks
changeset 72:e05701354b6e
Add notes from Day 2
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 02 Feb 2018 20:59:06 +0000 |
parents | 32f018861e36 |
children | 74976fddd25f |
files | 5-Erlang/README.txt |
diffstat | 1 files changed, 31 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/5-Erlang/README.txt Fri Feb 02 20:38:36 2018 +0000 +++ b/5-Erlang/README.txt Fri Feb 02 20:59:06 2018 +0000 @@ -33,4 +33,34 @@ Erlang can pack and unpack bits using "<<Variable:bit_count[ Variable:bit_count[, …]]>>" and then pattern matching to unpack -Functions are well documented at http://erlang.org/doc/apps/stdlib/index.html \ No newline at end of file +Functions are well documented at http://erlang.org/doc/apps/stdlib/index.html + +Case statements use patern matching: + +Animal = "dog". +case Animal of + "dog" -> underdog; + "cat" -> thundercat; + _ -> something_else +end. + +If statements use "guards" (conditions) and implies "elseif". Else requires a guard of "true". + +if + X > 0 -> positive; + X < 0 -> negative; + true -> zero +end. + +Anonymous functions are defined as "fun(Var[, …]) -> [code] end." + +List processing is in the "lists" module - e.g. lists:foreach/2 takes function and list of values and calls function on each value in list. + → There are also map, filter, all and any functions, etc. + → Interesting extra functions are lists:takewhile/2 and lists:dropwhile/2, which filter or discard from the start of a list based on the function's return + +Erlang has list comprehension - similar to Python's, but with Erlang-y syntax. + → [Function(Val) || Val <- List]. % Call Function on Val for each Val in List + → Can also be done without a function and just code - e.g. [X * 2 || X <- [1, 2, 3]]. + → Therefore, map(Func,List) can be defined as [ Func(X) || X <- List ]. + → EXCEPT it isn't that simple - there can be multiple clauses after the double-pipe, which can be generators (X <- List) or filter clauses (X < 3) + \ No newline at end of file