News for hackers. Stuff that matters.
Re:OCamlers' critiques of Haskell by bliti on Thursday August 27, 2015 @02:11AM (#10127803)
I've toyed with Haskell before (enjoy toying with languages in my spare time). Its a nice language, that has a good community. But for some reason I could not see myself using it as one of my main goto (no pun intended) languages. Not due to any technical reason. Mostly because there is no easy introduction into it. No small projects to undertake showing what practical uses it might have. No twitter clone using SQLite (laugh all you want, this type of tutorial project helps understand how the language should be used and showcases libraries). Its just seems purely about doing math with it. But Im probably wrong. Last time I looked into it was about a year ago. I'll gladly look again if anyone can comment about it.
Re:OCamlers' critiques of Haskell by Chlorus on Thursday August 27, 2015 @02:30AM (#10127857)
Have you checked out Real World Haskell by Bryan Sullivan? It's different than most Haskell resources I've found in that it introduces you to monads & IO fairly quickly, so you're not trapped with toy math problems. That was an issue I had with Learn You a Haskell for Great Good - finding the sum of all odd squares that are smaller than 10,000 is good and all, but not exactly a programming problem I have often...
Re:OCamlers' critiques of Haskell by radmuzom on Thursday August 27, 2015 @02:40AM (#10127895)
Note that the book is a bit dated, though most chapters are still "valid". See this question from Stackoverflow [1] - Which parts of Real World Haskell are now obsolete or considered bad practice?

[1] http://stackoverflow.com/questions/23727768/which-parts-of-r...

Re:OCamlers' critiques of Haskell by bliti on Thursday August 27, 2015 @02:43AM (#10127903)
Any alternatives available?
Re:OCamlers' critiques of Haskell by radmuzom on Thursday August 27, 2015 @02:49AM (#10127935)
This looks good but I have not personally tried the courses mentioned at the top of the article - https://github.com/bitemyapp/learnhaskell
Re:OCamlers' critiques of Haskell by acconsta on Thursday August 27, 2015 @03:42PM (#10131591)
I've had some success with this, published last year:

http://www.amazon.com/Beginning-Haskell-A-Project-Based-Appr...

Re:OCamlers' critiques of Haskell by bliti on Thursday August 27, 2015 @02:34AM (#10127873)
I have not. Will do so now. Thank you for recommending it. For anyone interested, here is the direct link to the online version of the book: http://book.realworldhaskell.org/read/
Re:OCamlers' critiques of Haskell by danidiaz on Thursday August 27, 2015 @02:43AM (#10127904)
I have found micro-web frameworks like Spock https://www.spock.li/ or Scotty are a good way to "get something started" in Haskell.

With that, "aeson" for JSON, "sqlite-simple" for persistence and perhaps something like "lucid" for HTML, you are half-way to that twitter clone... :)

Re:OCamlers' critiques of Haskell by evincarofautumn on Thursday August 27, 2015 @02:46AM (#10127919)
I’ve been fortunate enough to use Haskell professionally at 2 jobs, and it’s been my go-to language for hobby projects for a few years. I find it useful for infrastructural-type projects where people would ordinarily reach for a managed imperative language such as Java or C#.

The main advantage for me is in code reuse and refactoring, something that really shines for medium-to-large projects, and is unfortunately hard to showcase in tutorial style.

Getting started wasn’t easy, but as you imply, not really due to the language—mainly because the learning materials didn’t suit me. I couldn’t bring myself to finish Learn You a Haskell or any other tutorial, so I just used the language to build stuff and figured things out.

Been thinking about writing a book about my experiences, but it’s Yet Another Project I’ll have to make the time for…

Re:OCamlers' critiques of Haskell by mateuszf on Thursday August 27, 2015 @02:54AM (#10127948)
What do you mean by refactoring? Automated tools to do that? Why would manual refactoring be better than let's say in more dynamic languages?
Re:OCamlers' critiques of Haskell by rprospero on Thursday August 27, 2015 @09:47AM (#10129307)
Perhaps a bit of a different viewpoint, but one of the reasons that I keep coming back to Haskell is that it lets me refactor things things that I can't in other languages. Take the following C code:

    char* response(messy* message) {
      body* b = message->body;
      if(b==NULL) {
        return NULL;}
      char** lines = b->text;
      if(t==NULL) {
        return NULL;
      }
      if(b->line_count < 11) {
        return NULL;
      }
      return t[10];
    }
Writing all of those if statements grows tedious. A lazier programmer could just drop them, but then the program would crash whenever the program received malformed data.

With Hackell, though, I can refactor out the if statements and get the following equivalent code:

    response :: Messy -> Maybe String
    response message = do
      b <- body message
      lines <- text body
      lines `atMay` 10
I want to be clear on a couple of points. First, the Haskell code performs all of the same "null" and bounds checks that the C code made, so this is an apples-to-apples comparison. Furthermore, this isn't using exceptions, like a similar piece of C++ code might try. There's no stack unwinding and nothing can escape to contaminate the rest of your program. Finally, this isn't just a weird special case built into the language. With a minimal amount of effort, I could have each failure return a unique error Code.

    errorCode :: Maybe a -> b -> Either b a
    errorCode (Just value) _ = Right value
    errorCode Nothing code = Left code

    response :: Messy -> Either Int String
    response message = do
      b <- body message `errorCode` 1
      lines <- text body `errorCode` 2
      (lines `atMay` 10) `errorCode` 3
I don't know a way of doing that in any of the C style languages. In those languages, you're forced to add in another if statement every time your code touches message->body to ensure that you're not going to segfault. With Haskell, I can refactor away those if statements and I don't have that code being duplicated all throughout my project.

EDIT: improved readability of errorCode

Re:OCamlers' critiques of Haskell by mightybyte on Thursday August 27, 2015 @10:17AM (#10129512)
Just a small side note, you don't even need to write your errorCode function. That and a number of other useful functions are provided in the errors package (http://hackage.haskell.org/package/errors). Also, I like to use string error messages because they're easier to grep for. Here's what your response function would look like with those changes.

    response :: Messy -> Either String String
    response message = do
      b <- note "error in body" $ body message
      lines <- note "error in text" $ text b
      note "error in atMay" $ lines `atMay` 10
Re:OCamlers' critiques of Haskell by rprospero on Thursday August 27, 2015 @10:52AM (#10129750)
For anyone reading this, I want to confirm that mightybyte's code would be far more idiomatic. I would never use integer error codes in a Haskell program, but I wanted to make the clear connection to C idioms. Also, using the errors package would be more appropriate than rolling your own function there, but I wanted to emphasize that it was simple, two line function, and not any fancy wizardry that requires caring about category theory.

EDIT: rechecking the errors library, the final line would probably be more clear with

      atNote "Not enough lines" lines 10
Re:OCamlers' critiques of Haskell by axman6 on Thursday August 27, 2015 @12:00PM (#10130197)
I've used a similar construct in dealing with some JSON data recently, using Either String a:

    (?) :: Maybe a -> String -> Either String a
    Nothing ? s = Left s
    Just a ? _ = Right a


    foo :: Value -> Either String Int
    foo val = do
        nested <- val ^? key "foo" . key "bar" ? "Could not find foo.bar"
        mid <- nested ^? nth 2 . _Integral ? "Second element of foo.bar was not an Integral value"
        return mid
which has really helped deal with the stringly typed nature of JSON. I should also mention I stole the idea (and operator name) from one of the Haskell parser combinator libraries which allowed you to name a whole parser expression, so errors would say "could not find ident" instead of "Expected on of a,b,c,d,e,d,f,g,h...., found X"
Re:OCamlers' critiques of Haskell by AnimalMuppet on Thursday August 27, 2015 @12:54PM (#10130567)
> Furthermore, this isn't using exceptions, like a similar piece of C++ code might try. There's no stack unwinding and nothing can escape to contaminate the rest of your program.

It always escapes to contaminate the rest of your program. In the C code, it escapes as a returned NULL. In C++/Java, it would be a thrown exception. In Haskell, it's the Maybe.

The Maybe is pretty close to isomorphic to a checked exception. The caller either has to handle it, or to return a Maybe/declare that it throws an exception itself. At some layer, you have to handle the Nothing/catch the exception. The Nothing short-circuits part of the computation; so does the exception. From where I sit, they're playing almost exactly the same roles. (So is the NULL in C, but it's clumsier than either an exception or a Maybe.)

Re:OCamlers' critiques of Haskell by rprospero on Thursday August 27, 2015 @02:40PM (#10131209)
You're right that the Maybe and Either types are very similar to checked exceptions. And, from that perspective, you're absolutely right that it contaminates the rest of the code around above it, similar to a checked exception. However, I would subjectively argue that Maybe is the less clumsy alternative. For the lazy programmer, the easy way to handle exceptions is to ignore them and let upstream deal with it. Even for checked exceptions, ignoring the problem is just a throws statement away. My subjective argument, though, is that monads are just slightly awkward enough to handle that you're encouraged to deal with the problem, instead of just ignoring it. I'd much rather handle whatever condition occurred than constantly deal with liftM and >>=, so I make sure to handle my "execeptions" as soon as I have enough information, just to make my own life easier. Your mileage may vary.
Re:OCamlers' critiques of Haskell by GregBuchholz on Thursday August 27, 2015 @02:55PM (#10131302)
>I don't know a way of doing that in any of the C style languages

http://www.boost.org/doc/libs/1_49_0/libs/optional/doc/html/...

https://www.google.com/#q=maybe+in+c%2B%2B

...and also I couldn't resist...

    char* response(messy* message) {
        body* b = message->body;
        return b && (b->text) && b->linecount < 11 && b->text[10];
    }
...for a C-ish like language. And that's without creating a "do-notation" macro to erase the &&, as the Haskell do-notation erases the >>=.
Re:OCamlers' critiques of Haskell by evincarofautumn on Thursday August 27, 2015 @06:57PM (#10132675)
These aren’t quite the same. With “x && x->y”, you’re using a boolean to encode the assumption that “x” is valid, but with “x >>= getY”, the type system has a proof, which makes refactoring safer.
Re:OCamlers' critiques of Haskell by pjmlp on Thursday August 27, 2015 @02:59AM (#10127958)
> Why would manual refactoring be better than let's say in more dynamic languages?

The compiler won't let you generate an executable if you missed a code location.

Whereas in dynamic languages you never know if you have covered all the code locations.

Re:OCamlers' critiques of Haskell by mateuszf on Thursday August 27, 2015 @03:17AM (#10128007)
Ok, but it's the same feature as in other static typing languages, like C/C++/Java?
Re:OCamlers' critiques of Haskell by tome on Thursday August 27, 2015 @03:30AM (#10128046)
Yes, but pjmlp is massively understating the case. Haskell's type system and fine-grained level of compositionality is remarkably helpful when it comes to refactoring.
Re:OCamlers' critiques of Haskell by pjmlp on Thursday August 27, 2015 @05:58AM (#10128464)
Yeap, I was just presenting the broader picture, specially since in the enterprise space, unit tests have even lower priority than documentation.
Re:OCamlers' critiques of Haskell by dllthomas on Thursday August 27, 2015 @11:59PM (#10133672)
Although you can get some serious mileage out of C's type system (such as it is). Refactoring in Python is... broken. I can't imagine why anyone would use Python when they need their code to stay nimble.
Re:OCamlers' critiques of Haskell by haxeny1 on Thursday August 27, 2015 @12:09PM (#10130257)
I would recommend Michael Snoyman's book on yesod (http://www.yesodweb.com/) to get a practical understanding specially from a crud perspective. I found this book to be very helpful: http://chimera.labs.oreilly.com/books/1230000000929/pr01.htm...

For me, using yesod has been quite a revelation. All of the errors in db operations get caught during my cabal build. This link https://github.com/yesodweb/yesod/wiki/ghci is something i used to test my db code all within the repl cycle after setting some pg env variables. All in all, its way smoother than many other environments i have worked with.

Re:OCamlers' critiques of Haskell by mightybyte on Thursday August 27, 2015 @07:23AM (#10128724)
> No twitter clone using SQLite

How about a URL shortener instead of a Twitter clone?

https://github.com/ryantrinkle/memoise

Video here: https://vimeo.com/59109358

Re:OCamlers' critiques of Haskell by kevin_thibedeau on Thursday August 27, 2015 @08:30AM (#10128981)
> No small projects to undertake showing what practical uses it might have

Pandoc (http://pandoc.org/) is written in Haskell and its modular design makes it easy to peruse without understanding everything.

Re:OCamlers' critiques of Haskell by dllthomas on Thursday August 27, 2015 @02:10PM (#10131043)
I've reached for Haskell for a number of small, scripty projects and the experience has been good. Not much better or worse than a similarly small scripty thing would have been in any other language.

Haskell shines the brightest when refactoring a moderately sized project (I have been yearning for this, working in a smallish Python codebase), but nothing prevents using it for small things.