Mercurial > repos > other > SevenLanguagesInSevenWeeks
view 5-Erlang/day1.erl @ 103:98be775c533c default tip
An odd "non-determinism" example from StackOverflow
It is clever, but doesn't make much sense as to how it gets its results
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 14 Jul 2019 13:44:13 +0100 |
parents | 32f018861e36 |
children |
line wrap: on
line source
-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").