-
Notifications
You must be signed in to change notification settings - Fork 193
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
Describe prefix/infix functions and record field accessors in Chapter 3 #285
Conversation
I may have tried to do a little too much with this PR. |
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: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Co-authored-by: milesfrain <[email protected]>
Co-authored-by: milesfrain <[email protected]>
Co-authored-by: milesfrain <[email protected]>
Co-authored-by: milesfrain <[email protected]>
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)) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!)
There was a problem hiding this comment.
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.
Co-authored-by: milesfrain <[email protected]>
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
Wanted to avoid committing directly to your branch, so made some more edits in shaunplee#1 |
Additional edits to purescript-contrib#285
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
.