Skip to content

Instantly share code, notes, and snippets.

@Drake81
Created September 26, 2013 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drake81/6720880 to your computer and use it in GitHub Desktop.
Save Drake81/6720880 to your computer and use it in GitHub Desktop.
recursive word count in erlang
-module(words).
-export([start/0, wordcount/1, wordcount/2, count/1]).
% Count to 10
count(N) ->
if
N < 11 ->
io:format("~w~n", [N]),
count(N+1);
N > 10 ->
io:format("Ende: N > 10~n")
end.
% Count Words
wordcount(N) ->
wordcount(N,0).
wordcount(N,C) ->
if
N == "" ->
io:format("Words: ~w~n", [C+1]);
true ->
[CHAR|REST] = N,
% Debug Char Ausgabe
%io:format("char: ~w~n", [CHAR]),
if
CHAR == 32 ->
wordcount(REST,C+1);
true ->
wordcount(REST,C)
end
end.
% Die Startfunktion
start() ->
% Woerter zaehlen
TT = "Dies ist ein Test",
io:format("Zaehle Woerter in: ~s~n",[TT]),
wordcount(TT,0),
% Zaehle bis 10
io:format("~nZaehle bis 10:~n"),
count(1).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment