Last active
March 17, 2017 15:25
-
-
Save bobfirestone/a73034afa8987b62d480453cc50b8ddd to your computer and use it in GitHub Desktop.
Part 1 of my submission for the shapes portion of the 1st assignment of FUNCTIONAL PROGRAMMING IN ERLANG THE UNIVERSITY OF KENT
This file contains hidden or 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(shapes). | |
| -export([area/1, enclose/1, perimeter/1, tests/0]). | |
| % The area, enclose , and perimeter functions each take a tuple | |
| % formatted {shape, {dimensions}}. The functions pattern match | |
| % against the shape atom and use the dimensions to do the calculations. | |
| % At the bottom of the file there is a test/0 function that | |
| % calls functions to test the math of the various calculations | |
| % and uses the io library to format the output to verify the | |
| % functions work as expected. | |
| area({rectangle,{Length,Width}}) -> | |
| Length * Width; | |
| area({circle,{Radius}}) -> | |
| (Radius * Radius) * math:pi(); | |
| area({triangle,{A,B,C}}) -> | |
| % solving using Heron's Formula | |
| S = (A + B + C)/2, | |
| math:sqrt(S * (S-A) * (S-B) * (S-C)). | |
| enclose({rectangle,{Length,Width}}) -> | |
| {rectangle,{Length,Width}}; | |
| enclose({circle,{Radius}}) -> | |
| {rectangle,{Radius * 2, Radius * 2}}. | |
| perimeter({rectangle,{Length,Width}}) -> | |
| (Length * 2) +(Width * 2); | |
| perimeter({circle,{Radius}}) -> | |
| (Radius + Radius) * math:pi(); | |
| perimeter({triangle,{A,B,C}}) -> | |
| A + B + C. | |
| tests() -> | |
| test_area_rectangle(), | |
| test_area_circle(), | |
| test_area_triangle(), | |
| test_enclose_rectangle(), | |
| test_enclose_circle(), | |
| test_perimeter_rectangle(), | |
| test_perimeter_circle(), | |
| test_perimeter_triangle(). | |
| test_area_rectangle() -> | |
| Spec = (6 == area({rectangle,{2,3}})), | |
| test_output_formatter("test_area_rectangle", Spec). | |
| test_area_circle() -> | |
| Spec = (12.566370614359172 == area({circle,{2}})), | |
| test_output_formatter("test_area_circle", Spec). | |
| test_area_triangle() -> | |
| Spec = (6 == area({triangle, {3,4,5}})), | |
| test_output_formatter("test_area_triangle", Spec). | |
| test_enclose_rectangle() -> | |
| Spec = ({rectangle,{2,3}} == enclose({rectangle,{2,3}})), | |
| test_output_formatter("test_enclose_rectangle", Spec). | |
| test_enclose_circle() -> | |
| Spec = ({rectangle,{4,4}} == enclose({circle,{2}})), | |
| test_output_formatter("test_enclose_circle", Spec). | |
| test_perimeter_rectangle() -> | |
| Spec = (10 == perimeter({rectangle,{2,3}})), | |
| test_output_formatter("test_perimeter_rectangle", Spec). | |
| test_perimeter_circle() -> | |
| Spec = (6.283185307179586 == perimeter({circle,{1}})), | |
| test_output_formatter("test_perimeter_circle", Spec). | |
| test_perimeter_triangle() -> | |
| Spec = (6 == perimeter({triangle, {1,2,3}})), | |
| test_output_formatter("test_perimeter_triangle", Spec). | |
| test_output_formatter(Name, Spec) -> | |
| io:format(Name ++ ": " ++ atom_to_list(Spec) ++ "\n"). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment