Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

They are used pervasively in Haskell, less so in other functional languages.

In Haskell, you can't write a "Hello world" program without using monads, so you cant really avoid learning about them.

IMHO monads are only really useful in Haskell because it has specific built-in syntax sugar to support them. Without this syntax sugar, they would be very cumbersome to use. So it's not really the monad type per se which is interesting, it is the code style which the syntax sugar enables.



And the reason you can't write "Hello world" in Haskell without using a monad is that functions in Haskell are "pure", meaning they cannot have side effects like outputting to the console.

Preventing side effects, including reading and writing global state, helps prevent bugs and makes it easier to understand and refactor Haskell code. Some would argue that the extra layers of abstraction from category theory and unpredictable order and number of lazy evaluations can actually make it harder to understand and refactor Haskell code.

Anyway, in order to perform I/O in Haskell, you evaluate your pure functions as a sequence of actions that are executed by the Haskell runtime. The construct that helps you build the sequence of I/O actions and allows you to bind their intermediate values to arguments to be used by subsequent actions is called the 'IO' monad.


While it's true that `IO` in Haskell has a `Monad` instance, you don't really have to know that to do `IO` in Haskell. Certainly you don't need to know `Monad` in the abstract to use `IO` concretely.

I like Haskell's `do` notation, which is its syntax sugar for monads, but it's really not that bad without. For example:

    do name <- getLine
       putStrLn ("Hello " ++ name)
isn't really that much nicer than:

    getLine >>= \name-> putStrLn ("Hello " ++ name)
or even:

    getLine >>= \name->
    putStrLn ("Hello " ++ name)
The main reason that monads are important in Haskell is that programs that do IO simply are not functions in a mathematical sense. If Haskell were limited to functions, it wouldn't be able to do IO.


You do need to understand the mechanics of monads in order to do even simple IO. E.g you can write:

    do name <- getLine
       putStrLn ("Hello " ++ name)
But you cant write:

    do name <- "Buddy"
       putStrLn ("Hello " ++ name)
 
You have to write:

    do let name = "Buddy"
       putStrLn ("Hello " ++ name)
Now try to explain that without basically explaining what a monad is.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: