News for hackers. Stuff that matters.
Re:Haskell IO for Imperative Programmers by WilliamLP on Monday November 09, 2009 @02:57AM (#930577)
> safeRead s = catch (readFile s) $ \_ -> return ""

> Contrasting this with Java is left as an exercise for the reader.

String safeRead(String fName) {

    try { return readFile(fName); }

    catch (Exception e) { return ""; }
}

Just the same thing, except replace ASCII garbage with words? (It assumes I have a readFile function, but that's not such a terrible thing to have around...)

Re:Haskell IO for Imperative Programmers by jrockway on Monday November 09, 2009 @09:57AM (#930972)
Java's standard library has no way to read a file with one function.
Re:Haskell IO for Imperative Programmers by WilliamLP on Monday November 09, 2009 @02:44PM (#931588)
I am allowed to write my own function in Java. Having a basic file read in a static function library is more than reasonable. Should it be in the standard library? Yes, but it not being there isn't really a cost.
Re:Haskell IO for Imperative Programmers by zaphar on Tuesday November 10, 2009 @01:44AM (#932593)
It is a cost because you now have to carry around your static function with you wherever you code.

Either you 1. have it in a utils library that adds to the external dependencies of all your java projects. or 2. you copy and paste it everywhere.

Either way it's a cost however small that java has and other languages have.

Additionally your example doesn't really demonstrate the same feature the haskell example did, which was a stream that haskell through laziness handles transparently for you. Your readfile static function breaks the stream because it forces you to read the entire file into memory before returning the contents. That was sort of the point and your counter example in java demonstrates it beautifully.

An example that did what the haskell example did would require significantly more code.

Re:Haskell IO for Imperative Programmers by shrughes on Monday November 09, 2009 @03:06AM (#930582)
Yeah, basically.