Skip to content

Instantly share code, notes, and snippets.

@danielrhodeswarp
Created February 21, 2017 12:34
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 danielrhodeswarp/3ca12cc928e3ebf5d0c94e936bc8217f to your computer and use it in GitHub Desktop.
Save danielrhodeswarp/3ca12cc928e3ebf5d0c94e936bc8217f to your computer and use it in GitHub Desktop.
For FUNCTIONAL PROGRAMMING IN ERLANG course on FutureLearn.com
-module(first).
-export([double / 1, mult / 2]).
%-export([mult / 2]).
-export([area / 3, square / 1, treble / 1]).
mult(X, Y) ->
X * Y.
double(X) ->
mult(X, 2).
area(A, B, C) ->
S = (A + B + C) / 2,
math:sqrt(S * (S - A) * (S - B) * (S - C)).
% recycle 'mult'
square(A) ->
mult(A, A).
% recycle 'mult'
treble(A) ->
mult(A, 3).
-module(second).
-export([summat / 1]).
-export([hypotenuse / 2, perimeter / 2, area / 2]).
% it seems like, in this module, we can call functions in the 'first'
% module without needing to do anything special. I wonder if that is only
% because both modules are in the same folder?
% http://erlang.org/doc/man/math.html is a useful reference here!
% a test
summat(A) ->
first:treble(A).
% get size of hypotenuse (of a right triangle) from sizes of other two sides
% recycle first:square() and use built-in math:sqrt()
hypotenuse(A, B) ->
math:sqrt(first:square(A) + first:square(B)).
% get perimeter of a right triangle from sizes of the two short sides
% recycle hypotenuse()
perimeter(A, B) ->
A + B + hypotenuse(A, B).
% get area of a right triangle from sizes of the two short sides
area(A, B) ->
first:mult(A, B) / 2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment