Skip to content

Commit 7326fab

Browse files
committed
Add description of prefix functions versus infix operators.
1 parent 905410c commit 7326fab

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

text/chapter3.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,19 @@ Note that, just like for top-level declarations, it was not necessary to specify
547547

548548
## Infix Function Application
549549

550-
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.
550+
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:
551+
552+
```text
553+
> findEntry "John" "Smith" addressBook
554+
```
555+
556+
However, this chapter has also included examples of _infix_ functions, such as the `==` function in the definition of `filterEntry`, where the function is put _between_ the arguments. These infix operators are actually defined in the PureScript source as infix aliases for their underlying implementations. For example, `==` is defined as an alias for the prefix `eq` function with the line:
557+
558+
```haskell
559+
infix 4 eq as ==
560+
```
561+
562+
Likewise, 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.
551563

552564
This is equivalent to the usual application `head (filter filterEntry book)`
553565

@@ -560,7 +572,7 @@ apply f x = f x
560572
infixr 0 apply as $
561573
```
562574

563-
So `apply` takes a function and a value and applies the function to the value. The `infixr` keyword is used to define `($)` as an alias for `apply`.
575+
So `apply` takes a function and a value and applies the function to the value. The `infixr` keyword is used to define `$` as an alias for `apply`.
564576

565577
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.
566578

@@ -574,6 +586,35 @@ becomes (arguably) easier to read when expressed using `$`:
574586

575587
```haskell
576588
street $ address $ boss employee
589+
There are situations where putting a prefix function in an infix position as an operator leads to more readable code. One example is the `mod` function:
590+
```text
591+
> mod 8 3
592+
2
593+
```
594+
This is fine, but doesn't line up with common usage. Wrapping a prefix function in backticks (\`) lets you use a prefix function in infix position as an operator, e.g.,
595+
```text
596+
> 8 `mod` 3
597+
2
598+
```
599+
Likewise, wrapping an operator in parentheses lets you use it as a function in prefix position:
600+
```text
601+
> 8 + 3
602+
11
603+
604+
> (+) 8 3
605+
11
606+
```
607+
This allows for compact definitions of curried (or partially applied) functions based on infix operator functions, such as the `add2` function below:
608+
```text
609+
> add2 = (+) 2
610+
> add2 4
611+
6
612+
```
613+
Alternatively, operators can be partially applied by surrounding them with parentheses and using `_` as an operand:
614+
```text
615+
> add3 = (3 + _)
616+
> add3 2
617+
5
577618
```
578619

579620
## Function Composition

0 commit comments

Comments
 (0)