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

Brent Yorgey byorgey at seas.upenn.edu
Sun Dec 13 13:21:10 EST 2009


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.

-Brent


More information about the Beginners mailing list