Skip to content

Instantly share code, notes, and snippets.

@Codesleuth
Last active August 29, 2015 13:58
Show Gist options
  • Save Codesleuth/9960914 to your computer and use it in GitHub Desktop.
Save Codesleuth/9960914 to your computer and use it in GitHub Desktop.
FizzBuzz in Erlang to teach myself the syntax.
% Run tests with: erlc fizzbuzz.erl && erl -noshell -pa ebin -eval "eunit:test(fizzbuzz, [verbose])" -s init stop
-module(fizzbuzz).
-compile(export_all).
-include_lib("eunit/include/eunit.hrl").
fizzbuzz(N) when N rem 15 == 0 -> fizzbuzz(3) ++ fizzbuzz(5);
fizzbuzz(N) when N rem 3 == 0 -> "fizz";
fizzbuzz(N) when N rem 5 == 0 -> "buzz";
fizzbuzz(N) -> integer_to_list(N).
fizzbuzz_test_() ->
[?_assert(fizzbuzz(1) =:= "1"),
?_assert(fizzbuzz(2) =:= "2"),
?_assert(fizzbuzz(3) =:= "fizz"),
?_assert(fizzbuzz(4) =:= "4"),
?_assert(fizzbuzz(5) =:= "buzz"),
?_assert(fizzbuzz(6) =:= "fizz"),
?_assert(fizzbuzz(7) =:= "7"),
?_assert(fizzbuzz(8) =:= "8"),
?_assert(fizzbuzz(9) =:= "fizz"),
?_assert(fizzbuzz(10) =:= "buzz"),
?_assert(fizzbuzz(15) =:= "fizzbuzz"),
?_assert(fizzbuzz(16) =:= "16"),
?_assert(fizzbuzz(30) =:= "fizzbuzz")].
$ erlc.exe fizzbuzz.erl && erl -noshell -pa ebin -eval "eunit:test(fizzbuzz, [verbose])" -s init stop
======================== EUnit ========================
module 'fizzbuzz'
fizzbuzz:13: fizzbuzz_test_...ok
fizzbuzz:14: fizzbuzz_test_...ok
fizzbuzz:15: fizzbuzz_test_...ok
fizzbuzz:16: fizzbuzz_test_...ok
fizzbuzz:17: fizzbuzz_test_...ok
fizzbuzz:18: fizzbuzz_test_...ok
fizzbuzz:19: fizzbuzz_test_...ok
fizzbuzz:20: fizzbuzz_test_...ok
fizzbuzz:21: fizzbuzz_test_...ok
fizzbuzz:22: fizzbuzz_test_...ok
fizzbuzz:23: fizzbuzz_test_...ok
fizzbuzz:24: fizzbuzz_test_...ok
fizzbuzz:25: fizzbuzz_test_...ok
[done in 0.203 s]
=======================================================
All 13 tests passed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment