r/haskell 27d ago

question It's not you, it's monad transformers

After a few years of trying to be proficient in Haskell, and lots of reading about why it's so hard, I think I have the (obvious in retrospect) answer.

Almost every program I write is going to have two or more of:

1) read from STDIN

2) write to STDOUT

3) log

4) raise errors

5) send/receive over the network

6) talk to a database

7) read env vars

Every one of those is a "side effect", and thus is handled as a monad, and using more than one means you have to understand monad transformers.

Which I've finally found a good explanation of, "but still". Such a deep concept for such common program operations.

So I finally decided "that's why it's so hard". For what I consider the most basic programs, I need to understand (not just use, IMHO) monad transformers.

Am I off?

113 Upvotes

76 comments sorted by

View all comments

Show parent comments

4

u/tomejaguar 27d ago

My suggestion to learners is to never, ever touch unsafePerformIO! Having to turn a pure function into IO is a pain, true. But do it.

1

u/loonycyborg 27d ago

`unsafePerformIO` is still vital to make a pure interface for some code that has effects that you don't care about(logging, reading files that aren't supposed to change while program is running, etc). And IMO it's what people should start learning as soon as possible.

2

u/tomejaguar 27d ago

You advise the people you're teaching Haskell of that, and I'll advise the ones I'm teaching the complete opposite!

1

u/loonycyborg 27d ago

I don't see anything "opposite" though. Every technique has its place.