Created
September 26, 2013 21:31
-
-
Save Drake81/6720880 to your computer and use it in GitHub Desktop.
recursive word count in erlang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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