3 ms·
Being a 100000 year old lycanthropic C coder things like lazy evaluation, garbage collection etcetera make my head spin (a full 1440 degrees) .. But I have com
by NAFV_P 12y ago
Being a 100000 year old lycanthropic C coder things like lazy evaluation, garbage collection etcetera make my head spin (a full 1440 degrees) ..
But I have come across Milewski before since I started on C++, he has a clever way of explaining programming concepts while tending to avoid the usual "is a, has a".
A couple of months ago I gave Haskell a decent go, and I sort of got my head round the basics of being a lazy coder.
Take a never ending list:
let t=[1..]
Okay, fairly compact notation. I think of this code as declaring blueprints of the list in order to create it when someone demands it.
That's alright, as long as those blueprints take up substantially less space in memory than the list itself. In the example above, the entire list can be generated by a starting value and a function to append the previous value incremented by unity to the end of the list; the data exhibits the ability to be compressed infinitely. The same can be applied to many other simple lists, like the set of all square numbers. This is an efficient use of memory.
When the function required to create a certain list becomes comparable in size to the resultant list, this advantage is lost. Luckily, those lists are fairly rare - white noise would be a pathological example.
- dmytrish 12y agoWell, Haskell has rather helpful strictness analyzer in its compiler that tries to determine automatically where strictness has to be enforced. C++ implementation does not have this advantage, but a limited form of laziness still seems to be a good tool for code expressiveness.
- NAFV_P 12y ago> Well, Haskell has rather helpful strictness analyzer in its compiler that tries to determine automatically where strictness has to be enforced. I wasn't aware of that, cheers for the heads up.