Programming in Haskell Series. II: Getting Started
In this article we are going to see how to setup a Haskell development environment, and we are going to write our own "Hello Haskell" program. There are a number of things that are necessary for pretty much every language you want to work with:
We can do the same with GHCi:
- Compiler/Interpreter
- Source code editor/IDE
- Build system
- Documentation
Compiler/Interpreter
There are many Haskell compilers available, but the one that seems to be more widely used is GHC (the Glasgow Haskell Compiler).
One of the nice things about Haskell is that it can be interpreted as well as compiled. The GHC interpreter is call GHi. They normally come bundled together in the so called Haskell Platform. Installing the platform will get you both.
To install the Haskell Platform, go to http://www.haskell.org/platform/ and follow the steps for your particular platform. I use Debian Linux (version 7.0,Wheezy). The official Debian package repositories include a Haskell-platform version that, although is not the latest and greatest, is enough to get started. Installation in Debian is pretty simple:
> sudo apt-get install haskell-platform
To verify that everything was successfully installed execute the following commands:
> ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.6.3
> ghci
GHCi, version 7.6.3 [NG/7.6.3.5]: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> 3+3
6
Prelude>
Leaving GHCi.
The debate editor vs IDE has become a sort of religious programming battleground. In most cases there is no debate at all, just groups of angry people yelling at each other how awesome their choice is, and how much the guys on the other side are missing out. I won't contribute to it.
The Haskell wiki offers detailed information about IDEs, editors and tools for Haskell development. Personally I prefer using an editor, and my editor of choice is Emacs. As almost everything else emacs can be installed via apt-get. I am not going to lie to you, Emacs is not easy to use. There are gazillions of commands that need to be learnt, but once you get passed the initial stages, you won't be needing a mouse any more, and you'll be able to write and browse your code very very fast indeed. Emacs has a mode for Haskell, which was very creatively named haskell-mode. This mode can also be installed via apt-get:
> sudo apt-get install emacs
> sudo apt-get install haskell-mode
If you prefer Eclipse, there is a plugin for Haskell development. It is called Eclipse FP Plugin.
Build system
If you don't know what a build system is, and what it does for you, I recommend you read the build automation entry in the wikipedia. In most of this Haskell learning series we are going to use only packages that are included in the standard library. For that reason, the use of a build system will not be necessary, but you can use one if you want.
In the world of Haskell there is one build system that rules all the others. It is called Cabal, and comes bundled with the Haskell Platform.
Documentation
There is only one website you need to remember when it comes to Haskell documentation; Hoogle. Hoogle is an immensely powerful search engine for Haskell documentation.You can search method names, packages and so on, but you can also provide the type of a method ( a -> b -> c), and hoogle will show you all the methods with that type. Types are a very important concept in Haskell, and being able to search by type is a very useful feature.
Putting it all together: Hello Haskell!
1) Create a file called Main.hs with the following content:
module Main where
-- A simple expression that returns a String
hello :: String
hello = "Hello Haskell"
-- Executable's entry point. It's type IO denotes
-- that this method can have side effects, like writing
-- on the standard output.
main :: IO ()
main = putStrLn $ hello ++ " " ++ (reverse hello)
2) Compile it with GHC
> ghc Main.hs
[1 of 1] Compiling Main ( Main.hs, Main.o )
Linking Main ...
3) Execute
> ./Main
Hello Haskell lleksaH olleH
We can do the same with GHCi:
1) Open GHCi and type the following commands
> ghci
GHCi, version 7.6.3 [NG/7.6.3.5]: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> let hello = "Hello Haskell"
Prelude> putStrLn $ hello ++ " " ++ (reverse hello)
Hello Haskell lleksaH olleH
Conclusions
Now that we are all set for Haskell development it is time to start learning more about other fundamental concepts like:
- Lists
- Pattern Matching
- Functions and function composition
- Haskell type system
- ...
We will be talking about these topics in future installments of this learning series. Before finishing this article, I want to challenge you to implement a very simple programming exercise. I ẃill provide my own solution to it in the next Haskell Learning series article.
Problem: FizzBuzz
<< Home