comparison 5-Erlang/day1.erl @ 71:32f018861e36

Add Day 1 Erlang content
author IBBoard <dev@ibboard.co.uk>
date Fri, 02 Feb 2018 20:38:36 +0000
parents
children
comparison
equal deleted inserted replaced
70:4198fa4e0df4 71:32f018861e36
1 -module(day1).
2 -export([wordcount/1]).
3 -export([count_to_ten/0]).
4 -export([success_or_error/1]).
5
6 wordcount("") -> 0;
7 wordcount([_|[]]) -> 1;
8 % NOTE: We can't use " " here - because it is a string and hence an array, not a character.
9 % Instead we get a slightly ugly "dollar space"
10 % Note the funky extra match to handle multiple consecutive spaces (although we still don't handle leading or trailing spaces)
11 wordcount([$ |[$ |Tail]]) -> wordcount([$ |Tail]);
12 wordcount([$ |Tail]) -> 1 + wordcount(Tail);
13 wordcount([_|Tail]) -> wordcount(Tail).
14
15 count_to(1) -> io:fwrite("~B ", [1]);
16 count_to(N) -> count_to(N-1), io:fwrite("~B ", [N]).
17
18 count_to_ten() -> count_to(10), io:fwrite("~n").
19
20 success_or_error({error, Message}) -> io:fwrite("error: ~s~n", [Message]);
21 success_or_error(success) -> io:fwrite("success~n").