An Introduction to Programming with HaskellGetting Started with the Haskell Programming LanguageAug 6, 2009 Mark Alexander Bain
Any programmer looking for a concise and easy to understand programming language, that's also free and open source, doesn't need look any further than Haskell.
Most programming languages concentrate on how something is to be calculated, not what is to be calculated. The Haskell programming language is not like that, and that's because it's a functional programming language. Languages such as C++, Java and VB all require the programmer to code a sequence of commands. However, functional programming languages such as Haskell evaluate the whole program as a statement. This makes Haskell a very concise and easy to understand language. There are two other things that a Programmer needs to know about the Haskell programming language:
The first step, therefore, is to obtain the Haskell compiler. Installing the Haskell CompilerThe installer for the Glasgow Haskell Compiler (GHC) can be downloaded from the Haskell Wiki web site. GHC is both an interpreter and a compiler and so can be used interactively or as a batch compiler. Using GHC InteractivelyGHC can be started in interactive mode by either:
Haskell commands can then by typed directing into the GHC command line, for example: 1+1
or "Hello World"
The evaluated results will be output the the screen as shown in figure 1 at the bottom of this article. Compiling a Haskell ApplicationThe interactive mode of GHC is very useful. It allows the user to run code directly. However, the programmer will want to create executables for their Haskell applications. If they're going to do that then the code is slightly different to that used in the interactive mode. That's because the programmer must use the main command to identify the code to be run and the print command to produce an output: main = print "Hello World from Haskell"
If this code is saved into a text file (for example "helloworld.hs") then it can be compiled from the command prompt: ghc -o helloworld helloworld.hs
This will create an executable that a user can then run: helloworld
This simple program will display the words "Hello World from Haskell" on the screen (as can be seen in figure 2). Adding Functions to a Haskell ProgramThe program is not particularly useful at the moment, but it is very easy to add functionality to the program. For example, the following will calculate the square of a number: main = print (square 4)
This calls a user defined function: square :: Double -> Double
square n = n * n
Here the function has been defined as requiring a double as an input (that's the first "Double") and a double as an output (that's the second "Double). It's also worth noting that the order of the lines do not matter (as shown in figure 3). This is because the whole program is evaluated - it is not evaluated line by line. And this shows just how different Haskell is when compared to other programming languages. It gives the programmer a very simple and flexible language so that the programmer can bend the code to their needs instead of being constrained by any enforced and unnecessary structure.
The copyright of the article An Introduction to Programming with Haskell in Computer Programming is owned by Mark Alexander Bain. Permission to republish An Introduction to Programming with Haskell in print or online must be granted by the author in writing.
Related Articles
Related Topics
Reference
More in Technology
|