Last active
February 26, 2017 17:25
-
-
Save jason-carter/8d452fc610c2e6c854d2e1ae066d6a41 to your computer and use it in GitHub Desktop.
FutureLearn Functional Programming In Erlang 1.9: First Erlang Program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(first). | |
-export([double/1, mult/2, area/3, square/1, treble/1]). | |
mult(X,Y) -> | |
X*Y. | |
double(X) -> | |
mult(2,X). | |
area(A,B,C) -> | |
S = (A+B+C)/2, | |
math:sqrt(S*(S-A)*(S-B)*(S-C)). | |
square(X) -> | |
X*X. | |
treble(X) -> | |
X*3. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(first_tests). | |
-include_lib("eunit/include/eunit.hrl"). | |
-export([square_test/0, square_test2/0]). | |
square_test() -> | |
% problem with following is that it returns the | |
% result of the last equation. | |
1 = first:square(1), | |
4 = first:square(2), | |
9 = first:square(3). | |
square_test2() -> | |
% apparently these are using macros. | |
% returns ok if all succeeds, otherwise | |
% fails with expected and actual values - nice | |
?assertEqual(1, first:square(1)), | |
?assertEqual(4, first:square(2)), | |
?assertEqual(9, first:square(3)). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(second). | |
-export([hypotenuse/2, perim/2, area/2]). | |
hypotenuse(Side1, Side2) -> | |
math:sqrt(first:square(Side1) + first:square(Side2)). | |
perim(Side1, Side2) -> | |
% Could also have done this in one line (but liked breaking it over two): | |
% Side1 + Side2 + hypotenuse(Side1, Side2). | |
Side3 = hypotenuse(Side1, Side2), | |
Side1 + Side2 + Side3. | |
area(Side1, Side2) -> | |
% Should have used first:mult... | |
%(Side1 * Side2) / 2. | |
first:mult(Side1, Side2) / 2. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment