Computer Languages A to Z: Haskell

by Gary L. Ratliff Sr. (eronstuc)

In his address on the evening he received his Turing Award in 1977, John Backus praised the type of languages known as Functional Languages. This address was mentioned in my recent article on Fortran, and is officially known as: Can programming be liberated from the von Neumann style?: a functional style and its algebra of programs. Haskell is one example of such a language. He also created such a language himself, which was known as FP. Haskell is an Open Source language, and is available for many different platforms including: Unix, Mac, Windows, 64 bit Linux, 32 bit Linux, BSD, and Solaris.

There are many different versions of Haskell available, but the one we will be using in this article is GHC (The Glasgow Haskell Compiler.) This language, once installed, can be operated as a compiler or an interpreter.

To get a suitable version for the PCLINUXOS 2009-x versions we will go to the following web site:

On the download section, you will find two different files for the i386 versions of Linux. At the moment, the most current version of this file is version 6.10.4. You will notice that one is 72 megs, and the other is 74 megs. The name of the file to select for download is: ghc-6.10.4-i386-unknown-linux-n.tar.bz2 (72 MB). I have installed it on my copy of PCLinuxOS 2009.1, and my Gnome 2009.2, so I can verify that if you have either of these systems, this file will function properly. If you make an error and select the larger file thinking that it would have more features, you will receive the same surprise I did when I tried to install that version on my Gnome system. The 74 meg version, when being configured, will report that it can not determine which directory it is in and will not create the Makefile needed to compile this for your system.

Installing and Adjusting the GHC system

Installing the system is very easy. We do not need to be root to download and to configure the system. But, as the file we download expands when un-archived to an item requiring nearly 700 megs of space, we will want to delete it once it has been properly installed and is operational. So, log in as root once you have the file downloaded. However, for the entire install, as well as the correction of two matters, we will need to become, and stay, root for a while. (NOTE: it is not advisable to be logged onto the internet while running as root!)

su <enter password for root>
# tar -jxvf ghc<tab>
# cd ghc<tab>
# ./configure
# make
# make install

OK, since Linux uses the GNU version of tar, this is a shortcut which allows the system to be unarchived and untarred with one command. If the product were a zipped file, then the command tar -zxvf would perform the required unarchiving. If you wish to perform the unarchiving as a separate step, then the bunzip2 command would be used, followed by the tar command to extract the file.

The last item of information you will see when the make install finishes, is that the documentation for the product will be found in this location: "/usr/local/share/doc/ghc/index.html." This is true, so while still logged in as root, open this file in Firefox, using this command:

# firefox file:///usr/local/share/doc/ghc/index.html.

Now a web page will be displayed. If you begin to click on the provided links, you will discover that they all function properly, except for two which will produce a file not found (browser error 404). These two links are the one for the users guide, and the one for Cabal. Notice that the link for the users guide is trying to take us to: /usr/local/share/doc/users_guide/index.html. And, at present, there is not any directory in this structure for the users guide. Now we see why we remained root, as the creation of this directory would not be permitted as the normal user. Lets now make this directory:

# cd /usr/local/share/doc/ghc/
# mkdir users_guide

Now, if you are using several different screens, you would just click on the one which still has the Firefox browser open. Then, in the search box, search for ghc users guide. You should find an item mentioned as: The Glorious Glasgow Haskell Compilation System User's Guide. So download this item to your system. In the KDE version, it seems to be downloaded to the desktop, while in the Gnome version it was found in a Downloads folder. However, it may be that is just how I had the feature set on these two systems. In any event, once it has been located, rename it to index.html. Once this is done the first item to correct will be completed by moving this file to the users_guide area. Now we indeed do have: /usr/local/share/doc/users_guide/index.html.

The next item we wish to correct is the Cabal item. Here again the current reference is wrong. There is an item, Cabal, under the libraries folder, and the Cabal folder does contain an item index.html. I tried two methods to fix this problem and neither worked. One was to create a symbolic link to the proper location of the file, and the other was to make a copy and send it to that location. That would not work either.

The method which works is to simply edit the html source file. You may have kwrite in the KDE version, gedit in the Gnome version, or you may well have emacs, if you installed this in trying the features of elisp. However, to save the edited file, you will still need to be the root user. The file you wish to edit is: /usr/local/share/doc/ghc/index.html. All you want to do is locate the cabal line, and insert "libraries/" in front of Cabal. Then save the file and exit. Next, you will want to delete the folders for ghc and the archived files from the location they were downloaded to.

That fixes everything. So you can now return to being a normal user. You can begin learning Haskell and the most excellent starting place is the users' guide. Here I recommend that you have the users guide open in one screen area, and an active Haskell session in another. To start your Haskell session, enter the following command into a terminal:

$ ghci (to which the system will respond with:)
GHCi, version 6.10.4:  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.

You will soon discover that having an interpreted language which will also compile, is a very useful feature. Once you read some of the user's guide, you will begin to appreciate many of the features of this system. For starters, the system will allow you to set an editor to perform the edits of the programs you wish to compile. The system will also allow you to enter a shell program by executing :! <cmd>

However, do not try to use the su command to become root, as the system will get the first letter and skip to a new line so that you can never get the password correct. As I had not yet installed Emacs on the Gnome version, that was one of the first things I tried. Also a simple apt-get install for Emacs will not do, as this only installs the library files needed by Emacs. Synaptic should be used to select the version for X11, and perhaps also the elisp sources. However, once you have Emacs installed, you can edit and install your first Haskell program. The easiest method is to just copy this from the section of the users guide on compiling source. The file is named Main.hs. Here is a shot of this process. The example Main.hs file gives you a simple factorial function.


Notice that the math is not limited. The amount of precision is almost infinite. Also, when doing division, you will obtain an exact answer, even if it does seem obvious. For example 99/3 would yield 33. While 100/3 yields the answer 100/3. Also, note that once a program has been compiled, the prompt has been changed from Prelude> to Main*> If you look at the items before the Prelude> prompt, you will see that it loaded several packages for you. If, for example, you enter pi, you will find that it is defined for you to 15 decimal places.

One normally develops programs by trying out expressions in the interpreter. A really fun item is the reverse function. This takes a string and shows it in reverse order. For example the reverse of Gary is yraG. Now this is not limited to just a simple word, as the next screen shot will reveal. Just be certain to surround the string with double quotes. Here we see some of the immortal words of Abraham Lincoln altered by this Haskell function:


The operation of the compiler has a similar set of flags as the familiar gcc set of compilers. The file may be compiled into an assembler listing. Or, it may be compiled into an object file for perhaps later making into a library of Haskell functions. If you have it compiled from within interactive mode, as we just did with the Main.hs program, its functions will be added to those offered by the interperter, and be ready to start using. So we have installed the system and corrected its few faults with the documentation setup, and given some examples of its use. Now go exploring with this new language and enjoy.

Before leaving we will mention what Cabal is. There are rpm files in the Red hat system, which is what PCLinuxOS uses. Another popular system is deb files in the systems derived from Debian. This allows installing new software. In a similar manner, Cabal is a repository of Haskel packages which may be installed to the Haskell system.