<p dir="ltr">Another trick is lazy evaluation. Rather than building the actual data structure fully, Haskell creates a thunk, that is, it keeps a reference to a recipe that, when evaluated, will produce the desired value. And it does this recursively. So when you say let foo = Foo bar baz, neither bar nor baz are necessarily evaluated at all, you just get a recipe for a Foo based on two other recipes for its fields. Creating another Foo based on this one, but with one field swapped out for a different value, merely created a new recipe, and evaluating the new data structure will not descend into the field values that are no longer used in the recipe.</p>
<p dir="ltr">This stuff takes a while to settle in, but it's actually quite neat.</p>
<div class="gmail_quote">On Jul 14, 2016 8:27 AM, "Bardur Arantsson" <<a href="mailto:spam@scientician.net">spam@scientician.net</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 07/14/2016 08:07 AM, Christopher Howard wrote:<br>
> Hi again all. From some online research, I understand that operations on<br>
> complex immutable structures (for example, a "setter" function a -><br>
> (Int, Int) -> Matrix -> Matrix which alters one element in the Matrix)<br>
> is not necessarily inefficient in Haskell, because the (compiler?<br>
> runtime?) has some means of sharing unchanged values between the input<br>
> and output structure. What I am not clear on, however, is how this<br>
> works, and how you ensure that this happens. Could someone perhaps<br>
> elaborate, or point me to a really good read on the subject?<br>
><br>
<br>
<a href="https://en.wikipedia.org/wiki/Persistent_data_structure" rel="noreferrer" target="_blank">https://en.wikipedia.org/wiki/Persistent_data_structure</a><br>
<br>
See in particular the section "Trees". (It basically works the sameĀ  for<br>
records; just imagine that a record is a 2-level tree, where each<br>
field/label in the record is an edge from the "field label" to the<br>
"field value". Multiple levels of records work essentially work the same<br>
as multi-level trees do.)<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div>