Skip to content

Instantly share code, notes, and snippets.

@pfigue
Created November 15, 2011 17:58
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 pfigue/1367793 to your computer and use it in GitHub Desktop.
Save pfigue/1367793 to your computer and use it in GitHub Desktop.
Count the number of words of a string in Erlang
-module(count_words).
-export([count_words/2]).
count_words([32 | Tail], NumWords) -> count_words(Tail, NumWords);
count_words([Letter | Tail], NumWords) -> 1 + count_words2(Tail, NumWords);
count_words([], NumWords) -> NumWords.
count_words2([32 | Tail], NumWords) -> count_words(Tail, NumWords);
count_words2([Letter | Tail], NumWords) -> count_words2(Tail, NumWords);
count_words2([], NumWords) -> NumWords.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment