Skip to content

Instantly share code, notes, and snippets.

@rhnonose
Created March 6, 2017 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhnonose/9aaeadfdfc627bcd2e80e74dd4c2aefe to your computer and use it in GitHub Desktop.
Save rhnonose/9aaeadfdfc627bcd2e80e74dd4c2aefe to your computer and use it in GitHub Desktop.
FutureLearn functional programming erlang assignment 1
-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