Skip to content

Instantly share code, notes, and snippets.

@muhammednagy
Created February 25, 2017 19:12
Show Gist options
  • Save muhammednagy/d525fe701c9c064d188bffc25b611029 to your computer and use it in GitHub Desktop.
Save muhammednagy/d525fe701c9c064d188bffc25b611029 to your computer and use it in GitHub Desktop.
Week 1 assignment kent university functional programming in erlang
-module(assignment).
-export([perimeter/1,area/1,enclose/1,bits/1]).
perimeter({square,R}) ->
4 * R;
perimeter({triangle,A,B,C}) ->
A + B + C;
perimeter({circle,R}) ->
2 * math:pi() * R .
area({circle,R}) ->
math:pi() *R*R;
area({rectangle, Length, Width}) ->
Length * Width;
area({triangle, A, B, C}) ->
S = (A + B + C) / 2,
math:sqrt(S * (S - A) * (S - B) * (S - C)).
enclose({circle, R}) ->
{rectangle, R * 2, R * 2};
enclose({rectangle, W, H}) ->
{rectangle, W, H};
enclose(T = {triangle, A, B, C}) ->
Area = area(T),
Base = max(max(A, B), max(B, C)),
Height = (Area * 2) / Base,
{rectangle, Base, Height}.
bits(V) ->
bits(V, 0).
bits(0, C) ->
C;
bits(V, C) ->
bits( (V band (V - 1)), (C + 1)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment