Skip to content

Commit d736f83

Browse files
authored
Clarify wording in Chapter 4 problem descriptions (#207)
* rename factorizations function and clarify problem Rename the function factorizations to factorize. Clarify the problem description (in chapter 4) to make it clear that the solution must find the prime factorization. * clarify what components means in exercise (chapter 4)
1 parent bfc24f9 commit d736f83

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

exercises/chapter4/test/Main.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ Note to reader: Delete this line to expand comment block -}
109109
Assert.equal (sort [ [ 3, 4, 5 ], [ 5, 12, 13 ], [ 6, 8, 10 ] ])
110110
$ sort
111111
$ triples 13
112-
suite "Exercise - factorizations" do
112+
suite "Exercise - factorize" do
113113
test "Test small non-prime number" do
114114
Assert.equal [ 3, 2 ]
115-
$ factorizations 6
115+
$ factorize 6
116116
test "Test number that uses the prime numbers less than 10" do
117117
Assert.equal [ 7, 5, 3, 2 ]
118-
$ factorizations 210
118+
$ factorize 210
119119
suite "Exercise Group - Folds and Tail Recursion" do
120120
test "Exercise - allTrue" do
121121
assert "all elements true"

exercises/chapter4/test/no-peeking/Solutions.purs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,20 @@ triples n = do
5858
pure [ i, j, k ]
5959

6060
-- | Provide the prime numbers that, multiplied together, make the argument.
61-
factorizations :: Int -> Array Int
62-
factorizations n = factorizations' 2 n []
61+
factorize :: Int -> Array Int
62+
factorize n = factorize' 2 n []
6363
where
64-
factorizations' :: Int -> Int -> Array Int -> Array Int
65-
factorizations' _ 1 result = result
64+
factorize' :: Int -> Int -> Array Int -> Array Int
65+
factorize' _ 1 result = result
6666

67-
factorizations' divisor dividend result =
67+
factorize' divisor dividend result =
6868
let
6969
remainder = rem dividend divisor
7070
in
7171
if remainder == 0 then
72-
factorizations' (divisor) (quot dividend divisor) (cons divisor result)
72+
factorize' (divisor) (quot dividend divisor) (cons divisor result)
7373
else
74-
factorizations' (divisor + 1) dividend result
74+
factorize' (divisor + 1) dividend result
7575

7676
allTrue :: Array Boolean -> Boolean
7777
allTrue bools = foldl (\acc bool -> acc && bool) true bools

text/chapter4.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ This means that if the guard fails, then the current branch of the array compreh
384384

385385
1. (Easy) Write a function `isPrime` which tests if its integer argument is prime or not. _Hint_: Use the `factors` function.
386386
1. (Medium) Write a function `cartesianProduct` which uses do notation to find the _cartesian product_ of two arrays, i.e. the set of all pairs of elements `a`, `b`, where `a` is an element of the first array, and `b` is an element of the second.
387-
1. (Medium) Write a function `triples :: Int -> Array (Array Int)` which takes a number `n` and calculates all Pythagorean triples whose components are less than or equal to `n`. A _Pythagorean triple_ is an array of numbers `[a, b, c]` such that `a² + b² = c²`. _Hint_: Use the `guard` function in an array comprehension.
388-
1. (Difficult) Write a function `factorizations` which produces all _factorizations_ of an integer `n`, i.e. arrays of integers whose product is `n`. _Hint_: for an integer greater than 1, break the problem down into two subproblems: finding the first factor, and finding the remaining factors.
387+
1. (Medium) Write a function `triples :: Int -> Array (Array Int)` which takes a number `n` and returns all Pythagorean triples whose components (the `a`, `b` and `c` values) are each less than or equal to `n`. A _Pythagorean triple_ is an array of numbers `[a, b, c]` such that `a² + b² = c²`. _Hint_: Use the `guard` function in an array comprehension.
388+
1. (Difficult) Write a function `factorize` which produces the [prime factorization](https://www.mathsisfun.com/prime-factorization.html) of `n`, i.e. the array of prime integers whose product is `n`. _Hint_: for an integer greater than 1, break the problem down into two subproblems: finding the first factor, and finding the remaining factors.
389389

390390
## Folds
391391

0 commit comments

Comments
 (0)