Haskell

Danger! If you are happy with your current knowledge of programming languages, don’t read on – Haskell might be responsible for some serious defects in your motivation.

I was always some kind of programming language geek. I loved learning and playing around with all kinds of programming languages1. Currently I’m trying to learn Haskell. Even though I learned functional programming at my university (two semesters using Haskell), I didn’t really learn how to interact with the outside world. This is where things start to get messy, no matter how beautiful the language is.

Haskell has many good ideas and takes programming to the next step. Things like implicit concurrency, explicit concurrency, purely functional, laziness, polymorphism, and various evaluation schemes provide many ways to get our job done (I wish I could show you how to do that, in the meantime look at the tutorials at the end of the article or the Haskell Wiki).

For me, Haskell was one of the harder languages to learn. The pure functional core is easy to understand, but the crossover from the outside world is not as easy.

The reason, I think, is that Haskell provides quite a lot of syntactic sugar3 and it is incredible flexible. Another reason is that Monads seem to be quite hard to understand, and Haskell is an ancient word meaning “programming in monads”.

Haskell tries to encapsulate the dirty world in a special Monad2, a show-stopper for many Haskell beginners (I’ve included some good tutorials on Monads at the end of this article to get you started).

Reading a Haskell program is a lot easier than writing one. The error messages from the compilers are very different, and due to type inference often hard to understand. It might be that someone who didn’t spent his last 15 years programming in an imperative paradigm is better suited.

Anyway, after you understand Haskell, your programs are much smaller and more clean than in any other language. Haskell is very expressive, and you tend to think more about your problem than how to formulate it in you language.

Currently my only concern is memory efficiency – looking at the great programming languages shootout Haskell is quite good, but in my current understanding it’s not straight forward to write memory efficient programs in Haskell. Anyway, we have the options and the possibility to improve since we won’t produce much code! Of course there are cases, where Haskell is not the first choice.

Startup

Currently I can’t teach anyone anything about Haskell programming – I’m still learning, but I can help you to get started.

Organization

When we start a Haskell program, the main function of the Main module gets called, so our entry-point (lets call it main.hs) should look like

  
    module Main
    where

     main = do
       ...
  

Larger programs should be split into modules, just like Java’s package system, Haskell modules are hierarchic. For example if we’ve got a module Interlinked.Demo, we’ll put it into the directory Interlinked and call the file Demo.hs. (This is not required for the Main module!!)

Demo.hs would look like:

  
    module Interlinked.Demo
    where

      callMe = 10
  

to include the module in another module, we’ll have to import it:

  
    module Main
    where

     import Interlinked.Demo

     main = do
       putStrLn $ show callMe
  

this imports all functions from the Demo.hs into the current namespace. If we have a name-clash (two functions with the same name), we have several possibilities to remedy the problem:

  • hide the unused function import Interlinked.Demo hiding (callMe)
  • Qualify all calls Interlinked.Demo.callMe
  • Qualify only one package import qualified Interlinked.Demo as D, and call it D.callMe

Compiling and Running

To run the program I’d recommend using runhaskell for development and compiling it to native code for the distribution or profiling.

To run our program we simply type runhaskell main.hs (if our Main module is in main.hs), or we can compile the code into a single executable using ghc: ghc --make main.hs, the --make option compiles all dependencies (if needed) and links everything.

To sum it up:

  • runhaskell main.hs for development
  • ghc --make main.hs for distribution and profiling

We could also fire up a interactive shell and evaluate the methods selectively using ghci main.hs.

Profiling

Profiling is quite important, especially in Haskell because it’s easy to make some stupid mistakes. To profile our program we have to compile it using ghc -prof -auto-all --make main.hs, this adds profiling code to our program (our code isn’t recompiled if we didn’t change our files, so we may need to delete old object files by ourselves).

To run our program with profiling enabled, we start it with ./main +RTS -p. This generates a file (main.prof) containing the profiling results.

See the profiling documentation for more information on profiling.

Glasgow Haskell Compiler

The Glasgow Haskell Compiler is one of the better known Haskell compilers, Haskell itself is just a specification and there are many implementations (like Hugs and the York Haskell Compiler). All of them implement Haskell 98, but in the meantime many extensions have been proposed and are available in some of the compilers (most of them in GHC).

More on Haskell

Since I didn’t tell you how to program in Haskell, I should point out some interesting tutorials:

The API of Haskell is available at Haskell Hierarchical Libraries and on Zvon. There is also a nice (very nice!) search engine for the API: Hoogle.

Monads

The first two of the following tutorials give an easy to understand introduction (and a good metaphor) to Monads, and the third one (All about Monads) is the most complete one with quite a lot of background:

Other sources include the Haskell homepage and of course Google. If you like books more than online tutorials, check out The Haskell School of Expression, but it’s not meant for the beginner. The Haskell book for beginners seems to be Haskell. The Craft of Functional Programming. Recently (January 2007) a new book Programming in Haskell came up, this one is also targeted at the beginner.

1 To date I’ve at least written a (more or less) simple program in Assembler, Bash, C, C++, C#, Cobol, GWA Basic, Haskell, Java, JavaScript, Lisp, Mosel, Pascal, PL/I, Prolog, Python, Ruby, Smalltalk, XQuery, XSLT, and various presentation languages like LaTeX and HTML, but there are so much more to learn. I can’t remember most of them, but as far as I can remember is Smalltalk the one I had most fun with. Probably Assembler ranges very high in the fun-scale too – I used to write little TSR programs and install them on my schools computers… no harm done. Pascal and C were the languages I learned programming with, but GWA Basic was the very first language I used (on my Atari 2600 ST) :-).

2 Monads are used for a lot more than interacting with the world!

3 It’s certainly no syntactic minimalist like Lisp.

Update: I just found an interesting article with quite the same experiences like I had. Anyway, it’s good to see that it’s not completely my fault.

Truth emerges more readily from error than confusion.
— Francis Bacon

Comments

comments powered by Disqus