16
votes

I've been looking at Haskell lately and it seems like a very nice way to watch programming problems from an alternative point of view - alternative to my usual imperative (I have a strong C++ background) view, at least.

However, all the articles I see seem to deal with the same kinds of programming problems:

  • Parsers
  • Compilers
  • Numeric computation problems

I'd like to give Haskell a try myself, by writing some GUI application. Hence, I'm wondering: does it make sense to write event-driven systems like GUIs in a functional programming language like Haskell? Or is that a problem domain at which imperative languages excel? Unfortunately it still takes quite some time for me to switch my mind to 'functional' mode, so I have a hard time deciding argueing against or in favor of using a functional programming language for an event-driven system.

I'd also be interested in examples of GUI applications (or event-driven systems, in general) which are implemented in Haskell.

6
Gtk2hs is Gtk binding for Haskell. - Hai
You, Sir, are simply too polite: other people always ask whether Haskell can be used in the "real world" (whatever that is), thereby implying that Haskell programmers aren't "real programmers" (whatever that means). If you actually use those keywords instead of "event-driven", you will actually find quite a wealth of questions and answers here on StackOverflow. - Jörg W Mittag
What was the conclusion to this? Care to share the experience? :) - mihai

6 Answers

15
votes

Here's a couple of Google keywords for you:

  • Functional Reactive Programming (FRP), a programming paradigm for, well reactive (aka event-driven) programming in purely functional languages,
  • Leksah, a Haskell IDE written in Haskell,
  • Yi, an Emacs-like editor which replaces Lisp with Haskell as the implementation, configuration, customization and scripting language,
  • Super Monao Bros. (yes, you guessed it, a Jump&Run game)
  • Frag (First-Person Shooter)
  • Purely Functional Retrogames is a 4-part series of blog articles about how to write games in a purely functional language, explained using Pacman as the example. (Part 2, Part 3, Part 4.)
5
votes

xmonad is an X11 window manager written in Haskell.

Also, looking at how the various Haskell GUI Libraries are implemented may give some ideas of how interactive programs are made in Haskell.

4
votes

Here's an example using epoll to implement an event driven web server: http://haskell.org/haskellwiki/Simple_Servers

3
votes

Take a look at this wikibooks article, it's a basic wxHaskell tutorial. In particular see the Events section.

I recommend spending some quality time with Haskell and FP in general before jumping in to develop a fully-fledged application so you can get more familiarized with Haskell, since it's quite different from C++

3
votes

xmonad is event-driven -- see the main event handling loop, which takes messages from the X server, and dispatches to purely functional code, which in turn renders state out to the screen.

3
votes

"Functional reactive programming" was already mentioned, but it may seem complicated if you're looking at it for the first time (and if you're looking at some advanced articles, it will look complicated no matter how long have you studied it :-)). However, there are some very nice articles that will help you understand it:

  • Composing Reactive Animations by Conal Elliott shows a "combinator library" (a common programming style in functional languages) for describing animations. It starts with very simple examples, but shows also more interesting "reactive" bit in the second part.

  • Yampa Arcade is a more evolved demo of Functional Reactive Programming. It uses some advanced Haskell features (such as Arrows), but is still very readable. Getting it to actually run may be more complicated, but it is an excellent reading.

  • Haskell School of Expression by Paul Hudak is a book which teaches Haskell using multimedia and graphics (including some animations etc.). It is an excellent reading, but it takes more time as it is an entire book :-).

I find my way to functional programming through F#, which is a bit less "pure" compared to Haskell, but it gives you a full access to .NET libraries, so it is easy to use "real-world" technologies from a functional language. In case you were interested, there are a couple of examples on my blog.