Skip to content

Instantly share code, notes, and snippets.

@ltriant
Last active December 17, 2015 22:43
Show Gist options
  • Save ltriant/723bae418d66bb7ca37c to your computer and use it in GitHub Desktop.
Save ltriant/723bae418d66bb7ca37c to your computer and use it in GitHub Desktop.
-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