Skip to content

Instantly share code, notes, and snippets.

@EarlOfBDE
Created May 5, 2020 22:26
Show Gist options
  • Save EarlOfBDE/a2319641643223d2b67e5a22449a9873 to your computer and use it in GitHub Desktop.
Save EarlOfBDE/a2319641643223d2b67e5a22449a9873 to your computer and use it in GitHub Desktop.
Functional Programming in Erlang
-module(first).
-export([double/1,mult/2,area/3,square/1,treble/1]).
mult(X,Y) ->
X*Y.
square(X) ->
mult(X,X).
double(X) ->
mult(2,X).
treble(X) ->
mult(3,X).
area(A,B,C) ->
S = (A+B+C)/2,
math:sqrt(S*(S-A)*(S-B)*(S-C)).
-module(second).
-export([right_hypotenuse/2,right_perimeter/2,right_area/2]).
right_hypotenuse(A,B) ->
% The hypotenuse (C) of a right triangle comes from Pythagoras' theorem: C^2 = A^2 + B^2,
% so C = sqrt (A^2 + B^2)
math:sqrt(first:square(A)+first:square(B)).
right_perimeter(A,B) ->
% The perimeter of any triangle is the sum of the lengths three sides
C=right_hypotenuse(A,B),
A+B+C.
right_area(A,B) ->
C=right_hypotenuse(A,B),
first:area(A,B,C).
@elbrujohalcon
Copy link

This is a perfect solution.
Tip about comments: if you put them outside the function and use a @doc tag, edoc and other tools can later use them better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment