[Haskell-beginners] Re: Applicability of FP unchanging things to real world changing things

Magnus Therning magnus at therning.org
Mon Dec 14 09:50:59 EST 2009

On Sun, Dec 13, 2009 at 6:21 PM, Brent Yorgey <byorgey at seas.upenn.edu> wrote:
> On Sun, Dec 13, 2009 at 11:24:06AM +0100, Heinrich Apfelmus wrote:
>> In other words, the book in your example is not a real book, it's just a
>> few bytes in the computer that model it for the purpose of tracking
>> whether it's lent out or not. For instance, these few bytes can be a
>> unique ID and a possible  lend  function is
>>     lend :: BookID -> Library -> Library
> I should also point out that people coming from an OO or imperative
> background often look at a function like this and think it looks
> incredibly inefficient --- why throw away the entire *library* and
> build a new one *from scratch* when all we want to do is modify one
> little part of it?  This seems a huge price to pay for immutability!
> But this line of thinking confuses abstract semantics with concrete
> implementation (something that imperative programming unfortunately
> tends to encourage, in my experience).  The runtime is free to
> implement operations like "lend" any way it likes, as long as the
> abstract semantics are preserved.  In this particular example, most of
> the "new" Library will probably be shared with the "old" Library, and
> just the part that is changed needs to be updated (in fact, note that
> this is only possible because Library is immutable! =) So in terms of
> actual execution we get the same efficiency that we would have by
> mutating the Library, but we get the cognitive benefit of being able
> to *think* of it as if we have produced an entirely new Library.

Doesn't this immutability make some things very easy to implement?
For instance, it ought to be trivial to backup state to disk (for
recovery or rollback), one just hands it to a thread asynchronously
and then continues handling requests.  Doing that sort of thing with
mutable state requires a bit more work, I'd say.

>From watching a presentation on Happstack, I believe this is how it
achieves some of its nice features.


Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus@therning.org          Jabber: magnus@therning.org
http://therning.org/magnus         identi.ca|twitter: magthe

More information about the Beginners mailing list