Last active
December 17, 2015 22:43
-
-
Save ltriant/723bae418d66bb7ca37c to your computer and use it in GitHub Desktop.
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(xmas). | |
-export([tree/1]). | |
tree(Width, _, _) when Width < 3 orelse Width > 21 -> | |
error( { out_of_range, { min, 3 }, { max, 21 }, { input, Width } } ); | |
tree(Width, _, _) when Width rem 2 == 0 -> | |
error( { width_is_not_odd, Width } ); | |
tree(Width, Trunk, Leaves) -> | |
lists:foreach( | |
fun (A) -> | |
Indent = (Width - A) div 2, | |
io:format("~s~s~n", [ | |
string:copies(" ", Indent), | |
string:copies(Leaves, A) | |
] ) | |
end, | |
lists:seq(1, Width, 2) | |
), | |
Indent = (Width - 3) div 2, | |
io:format("~s~s~n", [ | |
string:copies(" ", Indent), | |
string:copies(Trunk, 3) | |
] ). | |
tree(Spec) -> | |
[ WidthStr, Trunk, Leaves ] = string:tokens(Spec, " "), | |
{ Width, _ } = string:to_integer(WidthStr), | |
tree(Width, Trunk, Leaves). | |
% 1> c(xmas). | |
% {ok,xmas} | |
% 2> xmas:tree("21 # *"). | |
% * | |
% *** | |
% ***** | |
% ******* | |
% ********* | |
% *********** | |
% ************* | |
% *************** | |
% ***************** | |
% ******************* | |
% ********************* | |
% ### | |
% ok |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment