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
+46-3Lines changed: 46 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -415,15 +415,58 @@ This brings the two arguments `entry` and `book` into scope – on the left-hand
415
415
416
416
## Curried Functions
417
417
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.
419
419
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`.
The `->` operator (in the type signature) associates to the right, which means that the compiler parses the type as
421
464
422
465
```haskell
423
466
Entry-> (AddressBook->AddressBook)
424
467
```
425
468
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`.
427
470
428
471
This means we can _partially apply_`insertEntry` by specifying only its first argument, for example. In PSCi, we can see the result type:
0 commit comments