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
Copy file name to clipboardExpand all lines: text/chapter3.md
+43-2Lines changed: 43 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -547,7 +547,19 @@ Note that, just like for top-level declarations, it was not necessary to specify
547
547
548
548
## Infix Function Application
549
549
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
+
infix4 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.
551
563
552
564
This is equivalent to the usual application `head (filter filterEntry book)`
553
565
@@ -560,7 +572,7 @@ apply f x = f x
560
572
infixr0 apply as $
561
573
```
562
574
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`.
564
576
565
577
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.
566
578
@@ -574,6 +586,35 @@ becomes (arguably) easier to read when expressed using `$`:
574
586
575
587
```haskell
576
588
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
+
>mod83
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:
0 commit comments