Mercurial > repos > other > SevenLanguagesInSevenWeeks
changeset 71:32f018861e36
Add Day 1 Erlang content
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 02 Feb 2018 20:38:36 +0000 |
parents | 4198fa4e0df4 |
children | e05701354b6e |
files | 5-Erlang/README.txt 5-Erlang/basic.beam 5-Erlang/basic.erl 5-Erlang/day1.beam 5-Erlang/day1.erl 5-Erlang/matching_function.beam 5-Erlang/matching_function.erl 5-Erlang/tail_recurse.beam 5-Erlang/tail_recurse.erl |
diffstat | 9 files changed, 85 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/5-Erlang/README.txt Fri Feb 02 20:38:36 2018 +0000 @@ -0,0 +1,36 @@ +Install Erlang with "sudo zypper install erlang" + +Erlang has a REPL (erl) + +Comments are "%", just to be different + +Commands need to end with a full-stop (like Prolog) + +Erlang has dynamic typing with coercion: 2 + 2 = 4, 2 + 2.0 = 4.0 + → BUT lists aren't just lists, they're strings when the values are printable ASCII 😮 + → AND numbers aren't coerced to strings + +As well as strings there are "atoms" - arbitrarily named symbols. + +Variables start with upper-case. + +Variables can't be redefined. Trying results in "exception error: no match of right hand side value <the value>" + +Lists use square brackets. Tuples use curly braces. + +Tuples are apparently used instead of maps and hashes to associate atoms as keys with values. + → Values are extracted from tuples using pattern matching + → BUT this implies matching ordering (which isn't the same as maps) + +Example: + Person = {person, {name, "Agent Smith"}, {profession, "Killing programs"}}. + % person, name and profession are atoms + {person, {name, Name}, {profession, Profession}} = Person. + Name. % prints "Agent Smith" + Profession. % prints "Killing programs" + +The initial atom is an Erlang-ism to make it easier to match all "person" tuples + +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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/5-Erlang/basic.erl Fri Feb 02 20:38:36 2018 +0000 @@ -0,0 +1,6 @@ +-module(basic). % Define the module name +-export([mirror/1]). % Note the Prolog-like notiation - "mirror function takes one parameter) +% Compilation is done from within "erl" using "c(module_name)" + +% This function will be called as "basic:mirror(thing)" +mirror(Anything) -> Anything. \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/5-Erlang/day1.erl Fri Feb 02 20:38:36 2018 +0000 @@ -0,0 +1,21 @@ +-module(day1). +-export([wordcount/1]). +-export([count_to_ten/0]). +-export([success_or_error/1]). + +wordcount("") -> 0; +wordcount([_|[]]) -> 1; +% NOTE: We can't use " " here - because it is a string and hence an array, not a character. +% Instead we get a slightly ugly "dollar space" +% Note the funky extra match to handle multiple consecutive spaces (although we still don't handle leading or trailing spaces) +wordcount([$ |[$ |Tail]]) -> wordcount([$ |Tail]); +wordcount([$ |Tail]) -> 1 + wordcount(Tail); +wordcount([_|Tail]) -> wordcount(Tail). + +count_to(1) -> io:fwrite("~B ", [1]); +count_to(N) -> count_to(N-1), io:fwrite("~B ", [N]). + +count_to_ten() -> count_to(10), io:fwrite("~n"). + +success_or_error({error, Message}) -> io:fwrite("error: ~s~n", [Message]); +success_or_error(success) -> io:fwrite("success~n"). \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/5-Erlang/matching_function.erl Fri Feb 02 20:38:36 2018 +0000 @@ -0,0 +1,11 @@ +-module(matching_function). +-export([number/1]). + +% Like Prolog, we can define different function matches as "overrides" of the function +% with the same name/parameter count but different "fixed" values +% For some reason these are separated by semi-colons here +number(one) -> 1; +number(two) -> 2; +number(three) -> 3. + +% Calling this with any value or atom other than one, two or three will fail. \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/5-Erlang/tail_recurse.erl Fri Feb 02 20:38:36 2018 +0000 @@ -0,0 +1,11 @@ +-module(tail_recurse). +-export([factorial/1]). +-export([fibonacci/1]). + +factorial(0) -> 1; +factorial(N) -> N * factorial(N-1). +% tail_recurse:factorial(2000). is fast to complete, but gives LONG output! Implies integers are huge + +fibonacci(0) -> 1; +fibonacci(1) -> 1; +fibonacci(N) -> fibonacci(N-1) + fibonacci(N-2). \ No newline at end of file