Last active
February 23, 2017 16:11
-
-
Save PeterHajdu/b50a96ccca5a6f7385b93392859b2498 to your computer and use it in GitHub Desktop.
erlang homework 1
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(shape). | |
-export([area/1, perimeter/1, bits/1]). | |
area({rectangle,{_, _}, W, H}) when (0<W) and (0<H) -> W*H; | |
area({circle,{_, _}, R}) when (R>0) -> R*R*math:pi(); | |
area({triangle,A, B, C}) when (A>0) and (B>0) and (C>0) -> heron(A, B, C). | |
heron(A,B,C) -> | |
S = (A + B + C)/2, | |
math:sqrt(S*(S-A)*(S-B)*(S-C)). | |
perimeter({rectangle,{_, _}, W, H}) when (0<W) and (0<H) -> 2*(W+H); | |
perimeter({circle,{_, _}, R}) when (R>0) -> 2*R*math:pi(); | |
perimeter({triangle,A, B, C}) when (A>0) and (B>0) and (C>0) -> A+B+C. | |
bits(N) when N >= 0 -> bits(N, 0). | |
bits(0, ACC) -> ACC; | |
bits(N, ACC) -> | |
SHIFTED_VALUE = N div 2, | |
INCREMENTED_IF_ONE = ACC + N rem 2, | |
bits(SHIFTED_VALUE, INCREMENTED_IF_ONE). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment