Skip to content

Instantly share code, notes, and snippets.

@lypborges
Created February 27, 2017 21:11
Show Gist options
  • Save lypborges/6d76873d2f3c3eab3cacfd3afc4f8127 to your computer and use it in GitHub Desktop.
Save lypborges/6d76873d2f3c3eab3cacfd3afc4f8127 to your computer and use it in GitHub Desktop.
-module(shapes).
-export([perimeter/1,area/1, bits/1]).
% Sample call, shapes:perimeter({square,{4}}) should return 16.
perimeter({square, {Side}}) ->
Side * 4;
% Sample call, shapes:perimeter({triangule,{4,3,5}}) should return 12.
perimeter({triangule,{SideA, SideB, SideC}}) ->
SideA + SideB + SideC.
% Sample call, shapes:area({triangule,{4,8}}) should return 16.0.
area({triangule,{Base, Height}}) ->
(Base * Height) / 2.
% http://www.wikihow.com/Convert-from-Decimal-to-Binary
% http://learnyousomeerlang.com/recursion
bits(Integer) -> bits(Integer,0).
bits(1, Acc) -> Acc + 1;
bits(Integer, Acc) ->
Remainder = (Integer rem 2) + Acc,
Rest = Integer div 2,
bits(Rest,Remainder).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment