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

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.


Not 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.


This only works if you are a top-down thinker. Many people are bottom-up thinkers.


Explain?

With "do" and the coincidental naming of "return", and IORefs if you insist, you can write imperative bottom-up code in Haskell.


    main = 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.


In 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!


On 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.


Well you could always follow the Unix system programming in OCaml tutorial:

http://ocamlunix.forge.ocamlcore.org/




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

Search: