From 67d0e54e91f0b47874765cb1d3249653738ab9ee Mon Sep 17 00:00:00 2001 From: eminencegrs Date: Sun, 21 Jan 2024 20:33:00 +0100 Subject: [PATCH] Add Chapter 01 08. --- src/FPinFSharp.App/Chapter_01/Chapter_01_08.fs | 9 +++++++++ src/FPinFSharp.App/FPinFSharp.App.fsproj | 1 + .../Chapter_01/Chapter_01_06_Tests.fs | 2 +- .../Chapter_01/Chapter_01_08_Tests.fs | 13 +++++++++++++ .../FPinFSharp.App.UnitTests.fsproj | 1 + 5 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/FPinFSharp.App/Chapter_01/Chapter_01_08.fs create mode 100644 tests/FPinFSharp.App.UnitTests/Chapter_01/Chapter_01_08_Tests.fs diff --git a/src/FPinFSharp.App/Chapter_01/Chapter_01_08.fs b/src/FPinFSharp.App/Chapter_01/Chapter_01_08.fs new file mode 100644 index 0000000..f9953c5 --- /dev/null +++ b/src/FPinFSharp.App/Chapter_01/Chapter_01_08.fs @@ -0,0 +1,9 @@ +namespace FPinFSharp.App.Chapter_01 + +// 1.8 Euclid’s algorithm (for computing the greatest common divisor of two natural numbers). +module Chapter_01_08 = + + let rec gcd (m, n) = + match (m, n) with + | (0, n) -> n + | (m, n) -> gcd(n % m, m) diff --git a/src/FPinFSharp.App/FPinFSharp.App.fsproj b/src/FPinFSharp.App/FPinFSharp.App.fsproj index 8b985fa..f160a20 100644 --- a/src/FPinFSharp.App/FPinFSharp.App.fsproj +++ b/src/FPinFSharp.App/FPinFSharp.App.fsproj @@ -13,6 +13,7 @@ + diff --git a/tests/FPinFSharp.App.UnitTests/Chapter_01/Chapter_01_06_Tests.fs b/tests/FPinFSharp.App.UnitTests/Chapter_01/Chapter_01_06_Tests.fs index 9dc2992..f3f26c7 100644 --- a/tests/FPinFSharp.App.UnitTests/Chapter_01/Chapter_01_06_Tests.fs +++ b/tests/FPinFSharp.App.UnitTests/Chapter_01/Chapter_01_06_Tests.fs @@ -6,7 +6,7 @@ open Xunit module Chapter_01_06_Tests = [] - let ``Should not throw an exception`` () = + let ``Should triple 10 to get 1000`` () = let x = 10 let expectedResult = 1000 let actualResult = validFunction x diff --git a/tests/FPinFSharp.App.UnitTests/Chapter_01/Chapter_01_08_Tests.fs b/tests/FPinFSharp.App.UnitTests/Chapter_01/Chapter_01_08_Tests.fs new file mode 100644 index 0000000..e95dd9b --- /dev/null +++ b/tests/FPinFSharp.App.UnitTests/Chapter_01/Chapter_01_08_Tests.fs @@ -0,0 +1,13 @@ +namespace FPinFSharp.App.UnitTests.Chapter_01 + +open FPinFSharp.App.Chapter_01.Chapter_01_08 +open Xunit + +module Chapter_01_08_Tests = + + [] + [] + [] + let ``Should get the greatest common divisor of two natural numbers`` (m, n, expectedResult) = + let actualResult = gcd(m, n) + Assert.Equal(expectedResult, actualResult) diff --git a/tests/FPinFSharp.App.UnitTests/FPinFSharp.App.UnitTests.fsproj b/tests/FPinFSharp.App.UnitTests/FPinFSharp.App.UnitTests.fsproj index 5929877..e8f2afa 100644 --- a/tests/FPinFSharp.App.UnitTests/FPinFSharp.App.UnitTests.fsproj +++ b/tests/FPinFSharp.App.UnitTests/FPinFSharp.App.UnitTests.fsproj @@ -15,6 +15,7 @@ +