Skip to content

Instantly share code, notes, and snippets.

@heronmedeiros
Forked from tafsiri/gist:1030863
Created July 26, 2011 02:48
Show Gist options
  • Save heronmedeiros/1105839 to your computer and use it in GitHub Desktop.
Save heronmedeiros/1105839 to your computer and use it in GitHub Desktop.
-module(day1).
-export([count_words/1]).
-export([upto/1]).
-export([printRet/1]).
% 1. Write a function that uses recursion to count the number of words in a string
list_len([]) -> 0;
list_len(Input) ->
[H | Tail] = Input,
1 + list_len(Tail).
count_words(Input) ->
{Status, Res} = regexp:split(Input, " "),
list_len(Res).
% 2. Write a function that uses recursion to count to ten
upto(Val, Target) when Val < Target -> %Guard is used to limit the terminating case
io:fwrite("~w~n",[Val]), %Print out val
upto(Val + 1, Target);
upto(Target, Target) ->
io:fwrite("~w~n",[Target]).
%Note upto/1 and upto/2 are completely diff functions
upto(Target) ->
upto(0, Target).
% 3. Write a function that uses matching to selectively print 'success'
% or 'error:message' given input of the form {error, Message} or success
printRet({error, Msg}) -> io:fwrite("error:~s~n",[Msg]);
printRet(success) -> io:fwrite("success~n").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment