Created
February 27, 2017 21:11
-
-
Save lypborges/6d76873d2f3c3eab3cacfd3afc4f8127 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(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