Saturday, May 2, 2009

[2009.04.02] Haskell and Eclipse [Part 2]

The post, Haskell and Eclipse, outlines how to set up the Haskell plugin in Eclipse. This is a follow up, illustrating how to get started developing in Haskell now that you have an IDE to work with.

Let's begin with just starting Eclipse and looking at some of what we have to work with as shown in Fig. 1.

Fig.1 Haskell Perspective of the Eclipse IDE

In order to start working with code, you need to create a project. Under the Navigator tab, right click and select New to see the options as shown in Fig. 2. Choose:
Haskell Project -> Name the project -> Select where to save it -> Finish Fig. 2 - 4 walk through this stage.

Fig.2 Create a new Haskell Project 1

Fig.3 Create a new Haskell Project 2

Fig.4 Create a new Haskell Project 3

Now take a look at the structure of the project you created. The src directory is where you will put your Haskell files. With the src directory selected, choose New and you will be given several options as to the type of files (or even a sub-project) you can create. See Fig. 5.

Fig.5 Creating different Haskell related files

Mostly, you will either create a Haskell Module or a standalone Haskell file. To create the standalone file, choose File, give it a name and the extension .hs.
NOTE: Both module and standalone file names should start with an uppercase letter. For example, I will create a new Haskell source file called AddIntList as shown in Fig. 6.

Fig.6 Creating a Haskell source file

For example, I will add code to the file to sum a list of integers. Once done, simply follow the points on the list which is also shown in Fig. 7.

  • Highlight all the code
  • Right click
  • Select Run As
  • Choose the compiler to use (I have GHC installed so pick GHCi)
Fig.7 Running Haskell code in Eclipse

NOTE: If you started a new project and then added a file rather than a new module, to compile, right-click and choose Run As on the actual project folder in the Navigation Pane, then when the GHCi window in Eclipse shows that the module loaded message (Ok, modules loaded:), then type in the name of the function and pass the arguments along as well.

The code used for this demo is as follows:

addIntList :: [Int] -> Int
addIntList x = foldl (+) 0 x

Once the code has successfully been compiled, you will see a message in the Console window indicating that. At the prompt, you can just type in the function you want to execute with arguments and hit enter as shown in Fig. 8.

Fig.8 Executing functions in the console window

Happy coding!


Anonymous said...

Could you paste in your code for the AddIntList exemple. Iam having some trouble getting the interactive GHCI window in the console window in eclipse.

Sparky Dasrath said...

Sorry about that, a while ago I was messing with Picasa and it deleted some of the images. I have added them back in as well as the little code snippet. Also I made a note about compiling a module vs. a new haskell file in eclipse. Let me know if you still have any issues getting it all to work.

Anonymous said...


thanks for the fast reply. It works now, but i used eclipse 3.5 EE edition and it didnot work with it.(I didnt get the GHCi in the console window(it did create a .exe file)). I downloaded the eclipse 3.4.1 Classic that you used and it worked fine with that.

Sparky Dasrath said...

no problem :-) I am glad you have it up and running