Skip to content

Commit

Permalink
Use math expressions (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmaro authored Aug 28, 2023
1 parent 402d5af commit f1b8d40
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion text/chapter10.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
6 changes: 3 additions & 3 deletions text/chapter11.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion text/chapter12.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion text/chapter4.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `+ 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
Expand Down
2 changes: 1 addition & 1 deletion text/chapter5.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f1b8d40

Please sign in to comment.