Skip to content

Commit e418ab4

Browse files
authored
[Chapter 3] Ease in into currying (#451)
1 parent c24f5e8 commit e418ab4

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

text/chapter3.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,58 @@ This brings the two arguments `entry` and `book` into scope – on the left-hand
415415

416416
## Curried Functions
417417

418-
Functions in PureScript take exactly one argument. While it looks like the `insertEntry` function takes two arguments, it is an example of a _curried function_.
418+
Functions in PureScript take exactly one argument. While it looks like the `insertEntry` function takes two arguments, it is an example of a _curried function_. In PureScript, all functions are considered curried.
419419

420-
The `->` operator in the type of `insertEntry` associates to the right, which means that the compiler parses the type as
420+
Currying means converting a function that takes multiple arguments into a function that takes them one at a time. When we call a function, we pass it one argument, and it returns another function that also takes one argument until all arguments are passed.
421+
422+
For example, when we pass `5` to `add`, we get another function, which takes an int, adds 5 to it, and returns the sum as a result:
423+
424+
```haskell
425+
add :: Int -> Int -> Int
426+
add x y = x + y
427+
428+
addFive :: Int -> Int
429+
addFive = add 5
430+
```
431+
432+
`addFive` is the result of _partial application_, which means we pass less than the total number of arguments to a function that takes multiple arguments. Let's give it a try:
433+
434+
> Note that you must define the `add` function if you haven't already:
435+
>
436+
> ```text
437+
> > import Prelude
438+
> > :paste
439+
>… add :: Int -> Int -> Int
440+
>… add x y = x + y
441+
>… ^D
442+
> ```
443+
444+
```text
445+
> :paste
446+
… addFive :: Int -> Int
447+
… addFive = add 5
448+
… ^D
449+
450+
> addFive 1
451+
6
452+
453+
> add 5 1
454+
6
455+
```
456+
457+
To better understand currying and partial application, try making a few other functions, for example, out of `add`. And when you're done, let's return to the `insertEntry`.
458+
459+
```haskell
460+
{{#include ../exercises/chapter3/src/Data/AddressBook.purs:insertEntry_signature}}
461+
```
462+
463+
The `->` operator (in the type signature) associates to the right, which means that the compiler parses the type as
421464

422465
```haskell
423466
Entry -> (AddressBook -> AddressBook)
424467
```
425468

426-
That is, `insertEntry` is a function that returns a function! It takes a single argument, an `Entry`, and returns a new function, which in turn takes a single `AddressBook` argument and returns a new `AddressBook`.
469+
`insertEntry` takes a single argument, an `Entry`, and returns a new function, which in turn takes a single `AddressBook` argument and returns a new `AddressBook`.
427470

428471
This means we can _partially apply_ `insertEntry` by specifying only its first argument, for example. In PSCi, we can see the result type:
429472

0 commit comments

Comments
 (0)