[1] https://www.reddit.com/r/haskell/comments/5h6emf/haskell_run...
[1] https://www.reddit.com/r/haskell/comments/5h6emf/haskell_run...
main =
runConduitRes -- dealing with finite resources
( sourceFileBS "input.txt" -- read input.txt as binary data
.| decodeC utf8 -- decode assuming UTF-8
.| linesC -- split into lines
.| mapC parseList -- parse each line into list of text
.| mapC (get 5) -- get sixth element of list
.| catMaybeC -- discard lines with no sixth element
.| encodeC utf8 -- encode as UTF-8
.| sinkFileBS "output.txt" -- dump into output.txt
)
And it's still very functional, if not more so!This will run in constant space and linear time, it will buffer reasonably, it will not leak file handles, it will gracefully clean up on exceptions, it will not crash when it fails to parse something correctly, and it makes the encoding assumption explicit (you cannot split into lines unless you know the encoding).
Dealing with I/O is not hard when you use the correct primitives.
I can come up with two explanations for this reliance on third-party libraries.
1) Haskell has always been a quickly evolving language attracting research-minded people which in turn go on to develop really cool libraries that are much better than conventional ways of doing things. The interpretation of this explanation is that it's simply not possible to keep the standard library up to date with the latest library developments.
It may also be the case that
2) Haskell has always been a really powerful language capable of offloading important tasks to libraries. What would need to be built-in functionality in other languages can be implemented as libraries with no sort of special treatment in Haskell, so people do it that way because they can, and because it keeps the base simple.
I can say that newcomers also seem to get a lot of advice that's directly at odds with how the Haskell community seems to think people should use the language. For example, Learn You A Haskell starts right off the bat by encouraging people to use the "list of characters" version of strings, even though that approach courts serious performance concerns.
I would guess those languages get built-in array syntax specifically because they have built-in arrays. The arrays come first, the syntax later.
> I don't think Haskell is worse than any other language in that regard.
Many Haskellers are happy about this kind of stuff:
min = head . sort
which is the very definition of a leaky abstraction. The blame lies with laziness, because it allows code to depend on implementation details of other code in crazy ways: "sort is O(n log n) unless you ask for only the first element, in which case it's O(n). What if you ask for the last element? Uhhh..."Python does in fact use a third-party library for efficient arrays (Numpy) and its use is so widespread as to be a defacto language unto itself.
There really isn't a serious attempt to build an industry-friendly programming language with functional features and laziness as a default. You'd make many different decisions than Haskell:
- No more language extensions, you'd fix the feature set.
- Better transparency on memory growth and GCs in tooling
- It'd be easier not to generate a ton of garbage, so the GC could be designed such that very large working sets could be handled with low latency.
- You wouldn't structure IO the way the have. Modern programs have demands for many, many side effects and threading an IO monad through every place they can touch is common practice, but not useful. A reduced power version of IO that would let developers push events to an IO-enabled functional reactor would be in the stdlib.
- Monad Transformers would almost certainly not be included, preferring: http://lambda-the-ultimate.org/node/4786
We shouldn't hold Haskell as the perfect expression of functional and lazy programming. It was never meant to be and it shows. It's just the tool we have right now to go to bat with.
In the meantime Haskell may well be the best option for various circumstances even with that flaw though. (And note that strict evaluation in non-total languages brings its own problems)
EDIT: It's not even a lazy IO problem since no lazy IO functions are used there.
To tell you the true, I can't understand why the code the OP talks about has a problem, and I can't even reproduce the problem (and never saw any of the problems of lazy IO in practice either). But I also can not say the code has no problem, and that is a big issue with the language.
I think there's a significant difference between undefined behavior (hello security flaws!) and the program crashing in a (reasonably) well-defined way.
That's what I get for not reading the code carefully...