Skip to content

Instantly share code, notes, and snippets.

@timgremore
Last active February 25, 2017 22:53
Show Gist options
  • Save timgremore/250df4219d19438e7add13ecbebe9168 to your computer and use it in GitHub Desktop.
Save timgremore/250df4219d19438e7add13ecbebe9168 to your computer and use it in GitHub Desktop.
Week-1: Shapes
%% Future Learn: Week 1
-module(shapes).
-export([perimeter/1, sum/1, area/1, area/2]).
-include_lib("eunit/include/eunit.hrl").
sum(L) -> sum(L, 0).
sum([H|T], A) ->
sum(T, H + A);
sum([], A) ->
A.
perimeter({triangle, L}) ->
shapes:sum(L);
perimeter(L) ->
shapes:sum(L).
area(W, L) ->
W * L.
area({triangle, B, H}) ->
(B * H) / 2;
area({circle, R}) ->
math:pi() * 2 * R;
area({rectangle, W, H}) ->
area(W, H).
perimeter_test_() ->
[
?_assertEqual(6, shapes:sum([1, 2, 3])),
?_assertEqual(14, shapes:perimeter([2, 3, 4, 5])),
?_assertEqual(9, shapes:perimeter({triangle, [2, 3, 4]})),
?_assertEqual(27, shapes:perimeter([2, 3, 4, 5, 6, 7])),
?_assertEqual(60, shapes:area(10, 6)),
?_assertEqual(30.0, shapes:area({triangle, 15, 4})),
?_assertEqual(22, round(shapes:area({circle, 3.5}))),
?_assertEqual(30, shapes:area({rectangle, 5, 6}))
].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment