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. getLine >>= putStrLn
case by doing greet name = putStrLn ("Hello, " ++ name)
main = getLine >>= greet
(Edited to include the definition of main) 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 >>=).It would be much better as
main = let greet name = putStrLn ("Hello, " ++ name)
in getLine >>= greet 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.
echo = forever $ getLine >>= putStrLn
This also works with do notation, if you'd want to make the greet example looping. greetLoop = forever $ do
name <- getLine
putStrLn ("Hello, " ++ name)
This is a very powerful concept. In mainstream imperative programming languages, you can't assign "statements" to variables or return them from functions or pass them as parameters. def forever
loop do yield end
end
forever do
name = gets
puts "Hello, name"
end
or even template <typename F>
void forever(const F& fn) { for (;;) fn(); }
int main()
{
forever([]
{
std::string str;
getline(std::cin, str);
std::cout << "Hello, " << str << '\n';
});
}
looks a lot like the above code to me.This becomes more apparent when using monads for non-IO applications like parsing. It's difficult (but not impossible, of course) to implement things like backtracking or non-determinism by writing lots of closures in an imperative programming language, they don't compose as nicely. There are parser combinator libraries (similar to Parsec) done in other languages of course, but some of them even implement monads in the language they work in. I recall seeing "class Monad" written in Python, I think it was in the pyparsec library.
A pointfree solution - if you want one - would be more in the form of:
getLine >>= putStrLn . ("Hello, " ++)
It's really mostly about getting used reading pointfree style, but
there's certainly an overuse of pointfree style in some Haskell code,
then it gets pointless.And my coworkers yesterday were complaining that ClojureScript looked like Brainfuck...
And if you did write code like that, Haskell's hlint tool would make suggestions to turn it neat and readable using fmap.
fmap ("Hello, " ++) getLine >>= putStrLn
This isn't necessarily better either, I'd write this using do-notation.I understood every bit of that code, I just think it's ugly and hard to read and there are better ways to get the same done.
"Here's how to read something and then print that: >>=". Sure, like `getLine >>= putStrLn`. Technically speaking, that's correct. But this doesn't generalize at all.
Maybe I want to greet the user by name. How do I read the name, prepend "Hello, " to it, and then print that? This is a totally natural thing to want to do, and is very straightforward in Java. But our Haskell newbie hits a wall - none of your cases cover it!
Here's how I might write that today, without using do notation: `getLine >>= (return . (++) "Hello, ") >>= putStrLn`. Look at that - it's totally nuts, and I would not attempt to explain that to a Haskell neophyte.
Using do-notation, you can make things nicer:
but this introduces a bunch of new syntax to learn: the difference between let-in and let and <-, how do-notation works, etc.I don't have a better approach than what you suggest. I suspect that this stuff simply can't be made easy.