Functional programming with Haskell
By Chris Dutton
This is yet another attempt to bring the ideas of functional programming to the masses here, and an experiment in finding ways to make it easy and interesting to follow.
Your feedback would be good as a means of judging my progress.
Why should you care?
Functional programming is fundamentally a very different way of thinking about programming. After all, we can learn several languages and pat ourselves on the back, but if the only real difference is syntactic, then we're not really being challenged.
What do I need?
You'll need either a Haskell interpreter or compiler. For learning purposes the Hugs Haskell interpreter is fine.
You can download it from: http://www.haskell.org/hugs/
It installs as easily as any other Windows program, and an entry will be created in the Start menu under: Start -> Programs -> Hugs98.
If you're running Windows 98 or ME, it would probably be wise to restart your computer, just to make sure everything the installer did takes effect. Windows 2000 and XP are pretty good about immediately applying the changes.
A quick look at Hugs
Start -> Programs -> Hugs98 -> Nov2003 -> Hugs (Haskell98 mode)
So, at startup of Hugs we've got some ASCII art, copyright information, some basic usage tips, and a prompt. This is a good start.
What can we do with this?
Well, we can evaluate expressions and see their results.
What's an expression?
An expression is a little bit of code that takes one or more values and gives you a new value.
Consider this very simple example.
But really, we could do basic math all day and be bored out of our skulls, so let's look at putting together a source file where we can build more complex things.
A Haskell source file is just a text file containing Haskell code. The extension we use is ".hs".
So, what will our source file contain? A simple hello world program.
module Main where main = putStrLn "Hello, world!"
What do we have here?
Well, first of all we have to deal with the fact that Haskell code is organized into modules. Modules allow us to easily reuse code in other programs, and they allow the actual language itself to be relatively simple. The name of the file should match the name of the module. Here the module is named "Main".
Next we have the "main" function, the center of activity, as in many other programming languages. The ease of creating this function shouldn't come as any surprise given the fact that Haskell focuses on functions.
putStrLn "Hello, world!"
Here we simply use the putStrLn function to print a string to the screen on its own line. The similar putStr function does the same, but doesn't automatically skip to a new line.
Testing the code
So, how do we run the code in this file?
Well, open up your trusty Command Prompt window and "cd" to the directory where you saved your Main.hs file.
Once you're there, start Hugs by simply typing "hugs" and hitting enter. Again we're back to:
To load the "Main" module we simply:
Prelude> :load Main Main>
The prompt has changed to indicate we're now in the Main module, rather than the Prelude module.
And to run the main function:
Main> main Hello, world! Main>
So, we've seen a little bit of Haskell
Is it scary?
If you say yes, that's not bad. New things can be scary. You'll get over it.
The real question, though, is: where do we go from here?
Well, since Haskell is a functional programming language, I'm thinking it might be good to see some more functions.
This site best viewed in a W3C standard browser at 800*600 or higher
Site design by Red Squirrel | Contact
© Copyright 2018 Ryan Auclair/IceTeks, All rights reserved