[Haskell-cafe] record update

Stephen Tetley stephen.tetley at gmail.com
Sat Sep 11 15:36:05 EDT 2010


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

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

Unfortunately, I haven't worked out how to do nested records in this style.


More information about the Haskell-Cafe mailing list