<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Two questions here, I think they might be related, perhaps even the same, but I am not sure, so I will ask both:<br><br>Q1: Re the function f below, I like the first implementation as it's "cleaner", but is the second implementation necessary for performance purposes?<br><br>-- g = some CPU intensive function<br><br>-- alternate 1<br>f a b = c + (g b)<br> where<br> c = dosomethingelse a (g b)<br> <br>-- alternate 2 <br>f a b = c + saveit<br> where<br> saveit = g b <br> c = dosomethingelse a saveit<br><br><br>Q2: Imagine that I might be creating a custom data structure. I need to
access both the data in that structure, but also "features" (don't know proper comp sci term) of the data structure. For instance, consider a Haskell list. The stuff in the list is the data, whereas the length of the list is what I am calling a "feature" here. Some of my features might be quite a bit more computationally intensive than "length", and may be referenced multiple times in many different places in my code. For performance purposes, should I consider modifying my data structure to embed these "features" as part of the data to ensure that it is only calculated once? Or can I rely on Haskell to do that for me? For instance, do I need to create the equivalent of:<br><br>data MyList a = MyList {mylist::[a], mylength::Int}<br><br><br><br>Thanks again for all your generous advice.<br></td></tr></table><br>