Created
March 6, 2017 19:24
-
-
Save rhnonose/9aaeadfdfc627bcd2e80e74dd4c2aefe to your computer and use it in GitHub Desktop.
FutureLearn functional programming erlang assignment 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(assignment). | |
-export([area/1, perimeter/1, enclose/1, bits/1, bits_tail/1]). | |
area({A,B,C}) -> | |
S = (A+B+C)/2, | |
math:sqrt(S*(S-A)*(S-B)*(S-C)); | |
area({A,B}) -> | |
A*B. | |
perimeter({A,B,C}) -> | |
A+B+C; | |
perimeter({A,B}) -> | |
2*(A+B). | |
% assumes A is the base of the triangle | |
enclose({A,B,C} = Triangle) -> | |
Height = area(Triangle) * 2 / A, | |
{A, Height}. | |
bits(N) when N > 0 -> | |
N rem 2 + bits(N div 2); | |
bits(_N) -> 0. | |
bits_tail(N) -> bits_tail(N, 0). | |
bits_tail(N, Acc) when N > 0 -> | |
bits_tail(N div 2, Acc + (N rem 2)); | |
bits_tail(_N, Acc) -> Acc. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment