News for hackers. Stuff that matters.
Re:IO Monad Considered Harmful by JoshTriplett on Friday January 23, 2015 @02:27AM (#8933788)
> Here's how I might write that today, without using do notation: `getLine >>= (return . (++) "Hello, ") >>= putStrLn`.

Don't write point-free code here. Here's a fairly simple version:

    getLine >>= \name -> putStrLn ("Hello, " ++ name)
The only novel thing there is the >>= operator itself. You don't even have to introduce return there; you can introduce return later.
Re:IO Monad Considered Harmful by rapala on Friday January 23, 2015 @02:45AM (#8933825)
Which can then be made to look exactly like the

  getLine >>= putStrLn
case by doing

  greet name = putStrLn ("Hello, " ++ name)
  main = getLine >>= greet
(Edited to include the definition of main)
Re:IO Monad Considered Harmful by ggchappell on Friday January 23, 2015 @02:54AM (#8933844)
This is a nice approach. Thanks to all of you in this thread. I think I'll try something like this when I introduce Haskell I/O in my Programming Languages class in a month or two.
Re:IO Monad Considered Harmful by millstone on Friday January 23, 2015 @03:22AM (#8933906)
If this is meant to be under main, you need a "let":

    main = do
      let greet name = putStrLn ("Hello, " ++ name)
      getLine >>= greet
For whatever reason, I didn't learn about the magic let syntax in do-notation until relatively late. Anyways this is very elegant, but not something I could have generated early on, especially because it mixes two different sequencing syntaxes (do-notation and >>=).
Re:IO Monad Considered Harmful by wz1000 on Friday January 23, 2015 @06:55AM (#8934425)
The 'do' is superfluous.

It would be much better as

    main = let greet name = putStrLn ("Hello, " ++ name)
                in getLine >>= greet
Re:IO Monad Considered Harmful by exDM69 on Friday January 23, 2015 @03:25AM (#8933909)
You can use "fmap" to make this nicer (but that introduces yet another concept). If you use hlint on the program above, you it will tell you to use fmap instead.

    fmap ("Hello, " ++) getLine >>= putStrLn
I learned this because hlint suggested it to me. Using hlint is a very good idea, even for beginners.

For a simple "greet" program, this isn't perhaps any neater or cleaner but fmap is a powerful tool with lots of practical uses.

Re:IO Monad Considered Harmful by millstone on Friday January 23, 2015 @02:36AM (#8933812)
Thanks, that's much better than what I wrote. Still I think that's a big jump for most programmers - it certainly was for me.