views:

526

answers:

6

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.

+4  A: 

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.

Justin Smith
Yes, I looked at the Haskell GUI Libraries already but had the odd feeling that they were 'merely' bindings to APIs which were designed with an 'imperative' mindset. I suspect that programmer with a 'functional' mindset might have a different approach to the same problem.
Frerich Raabe
"FG is an arrow-based high-level functional approach to composable GUIs" "FranTk uses behaviours and events, concepts from Conal Elliott’s Functional Reactive Animation" "Grapefruit is an arrow-based declarative library. Widgets, windows and control components communicate via discrete and continuous signals." -- these discriptions are not descriptions of "merely bindings".
Justin Smith
+3  A: 

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++

Mauricio Scheffer
You're right that it takes some time to get familiar with Haskell. However, I noticed that it's not as difficult if you've worked with template meta programming in C++ before. Template meta programming is very declarative, too and you get used to performing iterations etc. without using state (but recursion).
Frerich Raabe
+3  A: 

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.

Don Stewart
+8  A: 

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.)
Jörg W Mittag
Excellent link collection! Especially the last one is quite interesting. Thanks a ton!
Frerich Raabe
+3  A: 

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

Don Stewart
Neat! That's a nice little example - and it's probably written by some experienced Haskell, too. So chances that I'm picking up bad habits there are not as high as in other places. ;-)
Frerich Raabe
+2  A: 

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

Tomas Petricek
Ooh, very nice! Thanks for these links! Also, thanks for pointing out that F# is functional and allows access to .NET libraries!
Frerich Raabe