Skip to content

Instantly share code, notes, and snippets.

@jameswalton
Last active May 6, 2020 09:43
Show Gist options
  • Save jameswalton/c3e40af631c442aa4c03768e73f7945c to your computer and use it in GitHub Desktop.
Save jameswalton/c3e40af631c442aa4c03768e73f7945c to your computer and use it in GitHub Desktop.
Functional programming in Erlang course
-module(first).
-export([double/1, mult/2, area/3, treble/1, square/1]).
mult(X,Y) ->
X*Y.
square(X) ->
mult(X,X).
treble(X) ->
mult(3,X).
double(X) ->
mult(2,X).
area(A, B, C) ->
S = (A+B+C)/2,
math:sqrt(S*(S-A)*(S-B)*(S-C)).
-module(second).
-export([hypotenuse/2, perimeter/2, area/2]).
hypotenuse(A,B) ->
AB = first:square(A) + first:square(B),
math:sqrt(AB).
%% Perimeter of a right-angled triangle
perimeter(A,B) ->
A+B+(hypotenuse(A,B)).
%% Area of a right-angled triangle
area(A,B) -> A*B/2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment