4 ms·
For a purely functional language like Haskell, this would not be a very good advice. Any kind of I/O would involve monads and other imperative constructs. Bette
by arocks 14y ago
For a purely functional language like Haskell, this would not be a very good advice. Any kind of I/O would involve monads and other imperative constructs. Better implement an algorithm involving trees or graphs to better appreciate a functional language.
- scott_s 14y agoOn the contrary, any interesting program is going to have to do some I/O. So if you're want to do useful things with a language, I think it's a good thing to learn the basics of opening, reading and writing files. Also, you should take a look at the top comment in this thread.
- Hemospectrum 14y agomain = interact id This is a slightly crude implementation of cat in Haskell. As you can see, it is replete with scary monad plumbing that gets in the way of the actual functionality (copying stdin to stdout). I would explain further and implement more than what the blog post suggested but there's already a book (Real World Haskell) that does this with a number of other Unix commands.
- rodrigoavie 14y agoIn the post I only suggested cat and tree for examples. Other nice tools would be: ls, touch, rm, mkdir, rmdir and even cal. I just made a few more suggestions. I can't count how many neat, small tools are provided by Unix. It's a large ground to play in!
- shangaslammi 14y agoNot at all. You can start doing I/O in Haskell without knowing anything at all about monads by just treating the do-syntax as an imperative DSL. In fact, Bryan O'Sullivan (who wrote Real World Haskell) just held a tutorial session on Haskell a couple of weeks ago where people completely new to the language implemented simple Unix tools like "wc". I don't think monads were mentioned at all.
- 4ad 14y agoThis only works if you are a top-down thinker. Many people are bottom-up thinkers.
- anothermachine 14y agoExplain? With "do" and the coincidental naming of "return", and IORefs if you insist, you can write imperative bottom-up code in Haskell.
- pjmlp 14y agoWell you could always follow the Unix system programming in OCaml tutorial: http://ocamlunix.forge.ocamlcore.org/ http://ocamlunix.forge.ocamlcore.org/