[Haskell-cafe] record update

Henning Thielemann lemming at henning-thielemann.de
Sat Sep 11 15:45:49 EDT 2010


On Sat, 11 Sep 2010, Stephen Tetley wrote:

> On 11 September 2010 18:21, Jonathan Geddes <geddes.jonathan at gmail.com> wrote:
>
>>> someUpdate :: MyRecord -> MyRecord
>>> someUpdate myRecord = myRecord
>>>     { field1 = f $ field1 myRecord
>>>     , field2 = g $ field2 myRecord
>>>     , field3 = h $ filed3 myRecord
>>>     }
>
> Applicatively, using no additional libraries, is how I do it:
>
> updateAllThree :: MyRecord -> MyRecord
> updateAllThree = (\s a b c -> s { field1 = f a, field2 = g b, field3 = h c})
>                    <*> field1 <*> field2 <*> field3

Cute!

> Note - (<$>) is not used, and the left hand function has one extra
> argument. This style exploits the fact that (<*>) is the Startling (S)
> combinator.

It uses the Applicative instance for functions.


More information about the Haskell-Cafe mailing list