From d74cb1098f7d94e08dae8db029589094bdad3119 Mon Sep 17 00:00:00 2001 From: gemmaro Date: Sat, 26 Aug 2023 11:24:15 +0900 Subject: [PATCH] Use math expressions Enabled MathJax support[1]. [1] https://rust-lang.github.io/mdBook/format/mathjax.html --- book.toml | 1 + text/chapter10.md | 2 +- text/chapter11.md | 6 +++--- text/chapter12.md | 2 +- text/chapter4.md | 2 +- text/chapter5.md | 2 +- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/book.toml b/book.toml index 632de6c7b..064f1c813 100644 --- a/book.toml +++ b/book.toml @@ -6,3 +6,4 @@ src = "text" title = "PureScript by Example" [output.html] git-repository-url = "https://github.com/purescript-contrib/purescript-book" +mathjax-support = true diff --git a/text/chapter10.md b/text/chapter10.md index f59ee3d5f..fc2352cde 100644 --- a/text/chapter10.md +++ b/text/chapter10.md @@ -477,7 +477,7 @@ export const unsafeHead = arr => { ## Exercises -1. (Medium) Given a record that represents a quadratic polynomial `a*x^2 + b*x + c = 0`: +1. (Medium) Given a record that represents a quadratic polynomial \\( a x ^ 2 + b x + c = 0 \\): ```hs type Quadratic = { diff --git a/text/chapter11.md b/text/chapter11.md index 4ca6b84a0..2823e4ff1 100644 --- a/text/chapter11.md +++ b/text/chapter11.md @@ -342,15 +342,15 @@ Tuple 3 ["gcdLog 21 15","gcdLog 6 15","gcdLog 6 9","gcdLog 6 3","gcdLog 3 3"] ## Exercises 1. (Medium) Rewrite the `sumArray` function above using the `Writer` monad and the `Additive Int` monoid from the `monoid` package. - 1. (Medium) The _Collatz_ function is defined on natural numbers `n` as `n / 2` when `n` is even and `3 * n + 1` when `n` is odd. For example, the iterated Collatz sequence starting at `10` is as follows: + 1. (Medium) The _Collatz_ function is defined on natural numbers \\( n \\) as \\( n / 2 \\) when \\( n \\) is even and \\( 3 n + 1 \\) when \\( n \\) is odd. For example, the iterated Collatz sequence starting at \\( 10 \\) is as follows: ```text 10, 5, 16, 8, 4, 2, 1, ... ``` - It is conjectured that the iterated Collatz sequence always reaches `1` after some finite number of applications of the Collatz function. + It is conjectured that the iterated Collatz sequence always reaches \\( 1 \\) after some finite number of applications of the Collatz function. - Write a function that uses recursion to calculate how many iterations of the Collatz function are required before the sequence reaches `1`. + Write a function that uses recursion to calculate how many iterations of the Collatz function are required before the sequence reaches \\( 1 \\). Modify your function to use the `Writer` monad to log each application of the Collatz function. diff --git a/text/chapter12.md b/text/chapter12.md index 19ed187d3..8db14bbd0 100644 --- a/text/chapter12.md +++ b/text/chapter12.md @@ -558,7 +558,7 @@ and open `html/index.html`. You should see the Koch curve rendered to the canvas 1. (Easy) Try changing the various numerical constants in the code to understand their effect on the rendered system. 1. (Medium) Break the `lsystem` function into two smaller functions. The first should build the final sentence using repeated application of `concatMap`, and the second should use `foldM` to interpret the result. 1. (Medium) Add a drop shadow to the filled shape using the `setShadowOffsetX`, `setShadowOffsetY`, `setShadowBlur`, and `setShadowColor` actions. _Hint_: use PSCi to find the types of these functions. - 1. (Medium) The angle of the corners is currently a constant (`tau/6`). Instead, it can be moved into the `Letter` data type, which allows it to be changed by the production rules: + 1. (Medium) The angle of the corners is currently a constant \\( \\tau / 6 \\). Instead, it can be moved into the `Letter` data type, which allows it to be changed by the production rules: ```haskell type Angle = Number diff --git a/text/chapter4.md b/text/chapter4.md index b247ee106..4afc630dc 100644 --- a/text/chapter4.md +++ b/text/chapter4.md @@ -364,7 +364,7 @@ This means that if the guard fails, then the current branch of the array compreh 1. (Easy) Write a function `isPrime`, which tests whether its integer argument is prime. _Hint_: Use the `factors` function. 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. - 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. + 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 ^ 2 + b ^ 2 = c ^ 2 \\). _Hint_: Use the `guard` function in an array comprehension. 1. (Difficult) Write a function `primeFactors` 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 into two subproblems: finding the first factor and the remaining factors. ## Folds diff --git a/text/chapter5.md b/text/chapter5.md index c5c70c069..9747f822e 100644 --- a/text/chapter5.md +++ b/text/chapter5.md @@ -99,7 +99,7 @@ This example demonstrates that guards appear on the left of the equals symbol, s ## Exercises 1. (Easy) Write the `factorial` function using pattern matching. _Hint_: Consider the two corner cases of zero and non-zero inputs. _Note_: This is a repeat of an example from the previous chapter, but see if you can rewrite it here on your own. -1. (Medium) Write a function `binomial` which finds the coefficient of the x^`k`th term in the polynomial expansion of (1 + x)^`n`. This is the same as the number of ways to choose a subset of `k` elements from a set of `n` elements. Use the formula `n! / k! (n - k)!`, where `!` is the factorial function written earlier. _Hint_: Use pattern matching to handle corner cases. If it takes a long time to complete or crashes with an error about the call stack, try adding more corner cases. +1. (Medium) Write a function `binomial` which finds the coefficient of the \\( x ^ k \\)th term in the polynomial expansion of \\( ( 1 + x ) ^ n \\). This is the same as the number of ways to choose a subset of \\( k \\) elements from a set of \\( n \\) elements. Use the formula \\( n! \\: / \\: k! \\, (n - k)! \\), where \\( ! \\) is the factorial function written earlier. _Hint_: Use pattern matching to handle corner cases. If it takes a long time to complete or crashes with an error about the call stack, try adding more corner cases. 1. (Medium) Write a function `pascal` which uses [_Pascal`s Rule_](https://en.wikipedia.org/wiki/Pascal%27s_rule) for computing the same binomial coefficients as the previous exercise. ## Array Patterns