Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describe prefix/infix functions and record field accessors in Chapter 3 #285

Merged

Conversation

shaunplee
Copy link

@shaunplee shaunplee commented Jan 3, 2021

Fixes #281 and fixes #282 as proposed.

Also provides a preview of operator sections before @milesfrain's proposed changes to Chapter 4 for the exploration of flip.

@shaunplee
Copy link
Author

I may have tried to do a little too much with this PR.

text/chapter3.md Outdated Show resolved Hide resolved
text/chapter3.md Outdated
@@ -547,11 +579,23 @@ Note that, just like for top-level declarations, it was not necessary to specify

## Infix Function Application

In the code for `findEntry` above, we used a different form of function application: the `head` function was applied to the expression `filter filterEntry book` by using the infix `$` symbol.
Most of the functions discussed so far used _prefix_ function application, where the function name was put _before_ the arguments. For example, when using the `findEntry` function to search an `AddressBook`, one might write:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's pick a binary function (would also be good to link to that page) to compare and contrast infix vs prefix. For example insertEntry.

Then we can provide examples like this:

book1 = insertEntry john emptyBook
book2 = john `insertEntry` emptyBook
book3 = insertEntry john (insertEntry peggy (insertEntry ned emptyBook))
book4 = john `insertEntry` (peggy `insertEntry` (ned `insertEntry` emptyBook))

-- Shorter alias/synonym and also make it right-associative so we can drop parens.
infixr 5 insertEntry as ++
book5 = john ++ peggy ++ ned ++ emptyBook

-- Segue into how $ is another general way to drop parens
book6 = insertEntry john $ insertEntry peggy $ insertEntry ned emptyBook

Copy link
Member

@milesfrain milesfrain Jan 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option for series of examples is:

add5 x = 5 + x
add5 x = add 5 x
add5 x = (+) 5 x
add5 x = 5 `add` x
add5   = add 5

Described in #261 (comment)

Edit, I see you included some of this material further on.

text/chapter3.md Outdated Show resolved Hide resolved
Co-authored-by: milesfrain <[email protected]>
text/chapter3.md Outdated Show resolved Hide resolved
text/chapter3.md Outdated

But why would we want to use `$` instead of regular function application? The reason is that `$` is a right-associative, low precedence operator. This means that `$` allows us to remove sets of parentheses for deeply-nested applications.

For example, the following nested function application, which finds the street in the address of an employee's boss:

```haskell
street (address (boss employee))
_.street (_.address (_.boss employee))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something like:

book3 = insertEntry john (insertEntry peggy (insertEntry ned emptyBook))

book6 = insertEntry john $ insertEntry peggy $ insertEntry ned emptyBook

from the the sequence of examples proposed earlier might be better here.

text/chapter3.md Outdated
```
This allows for compact definitions of curried (or partially applied) functions based on infix operator functions, such as the `add2` function below:
```text
> add2 = (+) 2
Copy link
Member

@milesfrain milesfrain Jan 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readers might be asking: "Isn't this both equivalently compact, and easier to type?"

add2 = (+) 2
add2 = add 2     <------- I forgot to change the above to this in my original message.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. Maybe focusing on the inline function use case would help?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I forgot edit that snippet that I copied.

Not sure if your follow-up comment still applies.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less so, now that you've clarified that you meant that add 2 would work as well.

What do you think? Are there some better examples of use cases? Or just delete this bit and just go on to operator sections?

(By the way, thanks for the detailed review!)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use cases are pretty obscure, since you can always substitute the original function name. I guess if you wanted to use composeKleisliFlipped in a map, backwards fish (<=<) is nicer. Not a great example for Ch3 though.

If an explanation isn't necessary for understanding the exercise code, then it's probably safe to skip. Unfortunately, if a beginner encounters something like (+) elsewhere, they might have a tough time finding the corresponding reference, or even making sense of the terse explanation. To address the former, I'd like to make a syntax lookup table.

We also don't have a well-defined policy on the book's strategy and scope. There are opportunities to reduce duplication, but I also think it's useful to elaborate on concepts from the spec with concrete examples that involve the chapter code (for example with your explanation of property accessors).

And I appreciate all your help with improving the book as well.

text/chapter3.md Outdated Show resolved Hide resolved
milesfrain added a commit to milesfrain/purescript-book that referenced this pull request Jan 4, 2021
Wanted to make this a PR to a PR branch for easier commenting on proposed changes.
purescript-contrib#285
Wanted to make this a PR to a PR branch for easier commenting on proposed changes.
purescript-contrib#285
@milesfrain
Copy link
Member

Wanted to avoid committing directly to your branch, so made some more edits in shaunplee#1
If you merge that (pending any other revisions we want to make) it will be applied to this PR.

@milesfrain milesfrain merged commit c11f881 into purescript-contrib:master Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants