4 ms·
Since Haskell lists are lazy, they aren't necessarily in memory all at once. For example, the standard "getContents" function reads a file and returns a list o
by mbrubeck 18y ago
Since Haskell lists are lazy, they aren't necessarily in memory all at once. For example, the standard "getContents" function reads a file and returns a list of characters. But since it returns a lazily-evaluated list, you can read arbitrarily large files and they will be streamed efficiently as you consume the list.
This is the same laziness that lets Haskell operate on infinite lists like [1..]
- eru 18y agoYes, I guess you are right. I have been bitten a few times by questions of memory before, so I was cautious in my answer. There's still a lot for me to learn about how lazyness impacts memory requirements. (Some times Haskell was lazier than I was expecting and kept thunks in memory that were bigger than their eventual result.)
- silentbicycle 18y agoWhile it probably wasn't clear, that's what I was wondering about when I asked about using streams. I find using lazy evaluation by default confusing sometimes, so I think about it terms of using streams or iterators. (Or are they called generators? The kind of function that, when called, returns either the next value or an end-of-stream sentinel.)
- eru 18y agoPython calls them generators. Haskell lists display somewhat similar behaviour. Although I still have to learn a lot, lazyness by default brings more benefits than it costs IHMO.