You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Chapter 2 - Add anchors for code snippets
* move a comment from the code to the mdbook text, since it shouldn't exist in the final version that learners work with
* Anchors for all final code blocks for chapter 3
* add a caution comment to maintainers about an implementation that is copied instead of anchored
* another maintainer caution code comment
* Rewording a bit on infix functions for clarity
Co-authored-by: milesfrain <[email protected]>
Co-authored-by: milesfrain <[email protected]>
@@ -233,29 +227,21 @@ The only exception to this rule is the `where` keyword in the initial `module` d
233
227
A good first step when tackling a new problem in PureScript is to write out type definitions for any values you will be working with. First, let's define a type for records in our address book:
This defines a _type synonym_ called `Entry` - the type `Entry` is equivalent to the type on the right of the equals symbol: a record type with three fields - `firstName`, `lastName` and `address`. The two name fields will have type `String`, and the `address` field will have type `Address`, defined as follows:
Note that `List Entry` is not the same as `Array Entry`, which represents an _array_ of entries.
@@ -301,24 +287,19 @@ PureScript's _kind system_ supports other interesting kinds, which we will see l
301
287
Let's write our first function, which will render an address book entry as a string. We start by giving the function a type. This is optional, but good practice, since it acts as a form of documentation. In fact, the PureScript compiler will give a warning if a top-level declaration does not contain a type annotation. A type declaration separates the name of a function from its type with the `::` symbol:
This type signature says that `showEntry` is a function, which takes an `Entry` as an argument and returns a `String`. Here is the code for `showEntry`:
This function concatenates the three fields of the `Entry` record into a single string, using the `showAddress` function to turn the record inside the `address` field into a `String`. `showAddress` is defined similarly:
A function definition begins with the name of the function, followed by a list of argument names. The result of the function is specified after the equals sign. Fields are accessed with a dot, followed by the field name. In PureScript, string concatenation uses the diamond operator (`<>`), instead of the plus operator like in JavaScript.
@@ -369,14 +350,13 @@ Let's also test `showEntry` by creating an address book entry record containing
369
350
Now let's write some utility functions for working with address books. We will need a value which represents an empty address book: an empty list.
This type signature says that `insertEntry` takes an `Entry` as its first argument, and an `AddressBook` as a second argument, and returns a new `AddressBook`.
This process is called _eta conversion_, and can be used (along with some other techniques) to rewrite functions in _point-free form_, which means functions defined without reference to their arguments.
@@ -555,7 +534,7 @@ We also know that we will need a function to pass to `filter`. Let's call this f
555
534
Putting these facts together, a reasonable type signature for our function, which we will call `findEntry`, is:
This type signature says that `findEntry` takes two strings, the first and last names, and a `AddressBook`, and returns an optional `Entry`. The optional result will contain a value only if the name is found in the address book.
@@ -602,7 +581,7 @@ There are situations where putting a prefix function in an infix position as an
602
581
2
603
582
```
604
583
605
-
This is fine, but doesn't line up with common usage (in conversation, one might say "eight mod three"). Wrapping a prefix function in backticks (\`) lets you use that it in infix position as an operator, e.g.,
584
+
The above usage works fine, but is awkward to read. A more familiar phasing is "eight mod three", which you can achieve by wrapping a prefix function in backticks (\`):
606
585
607
586
```text
608
587
> 8 `mod` 3
@@ -715,8 +694,7 @@ We can rewrite the right-hand side of `findEntry` using either operator. Using b
715
694
In this form, we can apply the eta conversion trick from earlier, to arrive at the final form of `findEntry`:
0 commit comments