Before reading further... Are you looking for great Linux hosting from a company that cares about GNU/Linux? Pick Dreamhost hosting, get a 10% bonus to the disk space (and support Free Software Magazine in the meantime!)
Haskell
A very different language
Download the whole article as PDF
- 2005-06-29
- Server side | Intermediate
-
Write a full post in response to this!
Many programmers are fluent in several programming languages. Most of these languages have some things in common. Loops and variables are fundamental features of most languages.
I want to show you a different way of solving problems. Haskell takes a different approach than you’re used to—to just about everything.
Why Haskell is interesting?
There are quite a few things about Haskell that make it interesting and unique:
- Haskell has no loops because it doesn’t need them. There is no “for” or “while” in Haskell.
- Haskell has no equivalent of the variables that you’re used to; it doesn’t need them either.
- Haskell is a functional language. In a language like Java or Python, your primary view of the world is an object. In Haskell, your primary view of the world is a function. I like to say that Haskell manipulates functions with the same ease that Perl manipulates strings. In Haskell, it’s commonplace to pass around bits of code. This is a powerful concept.
- Haskell functions are also pure. Every time they’re called with the same arguments, they’ll return the same result. Functions in most languages can return different results each time they’re called. The results may depend on things like a global counter or I/O. Haskell functions also have no side-effects. They won’t stomp over a global variable.
- Haskell is a lazy language. It never performs a computation unless it needs to. This is not just an optimization; it’s a powerful way to view the world. Code that could be infinite loops or consume vast amounts of memory in other languages are simple, everyday tools in Haskell.
- Haskell can be either interpreted or compiled to native machine code. It also interfaces easily with C. You can call C functions from Haskell with a minimum of hassle. Usually, you’ll only need 2 or 3 lines of code to accomplish the call. Haskell also has interfaces to Java, .NET, and Python.
Haskell takes a different approach than you’re used to—to just about everything
- Haskell lets you write code in a surprisingly intuitive way. Reading Haskell code is easy, and reasoning about Haskell code is easy, too. You’ll have less need for a debugger with Haskell.
To get you started, here’s an example for a simplistic grep, written in Haskell:
import MissingH.List
main = do
c <- getContents
putStr (unlines(filter (\line -> contains "Haskell" line)
(lines c)))
This will simply read data from standard input and display all lines containing the word “Haskell” on standard output. I’ll go through this example in more detail and show how it works later in this article.
The Haskell toolbox
To get started with Haskell, you’ll need a compiler or interpreter. The most popular compiler is GHC, available from the GHC web site. Some Linux or BSD distributions have GHC packages available; look for a package named ghc or ghc6. If your operating system doesn’t have packages available, sources and binaries for many systems are available from the GHC homepage.
The GHC package actually includes a compiler (ghc) and an interpreter (ghci). Use whichever you like. If you prefer a smaller package that includes only an interpreter, try Hugs also from the HUGS web site. Many distributions also contain Hugs packages.
Both GHC and Hugs come with a basic library of Haskell code called fptools. A reference is available from GHC’s site.
Haskell manipulates functions with the same ease that Perl manipulates strings
The examples in this article will also use functions from MissingH, a library of useful functions written in Haskell. Many other Haskell libraries are also available for use. See the links at the end of this article for more information.
To compile a Haskell program with ghc, you could use a command such as
ghc --make -o program program.hs
The examples here use MissingH, so you’ll need to add
-package MissingH
at the beginning of your ghc command line. You can run Haskell programs with Hugs by saying
runhugs program.hs
Laziness at work
The grep example at the beginning of this chapter probably doesn’t make much sense yet. Here’s another version of it that does exactly the same thing, but breaks down the code into more manageable pieces:
import MissingH.List filterfunc line = contains "Haskell" line main = do c <- getContents let inputlines = lines c let outputlines = filter filterfunc inputlines let outputstring = unlines outputlines putStr outputstring
Write a full post in response to this!
Similar articles
Do you like this post?
Vote for it!
Copyright information
This article is made available under the "Attribution-Sharealike" Creative Commons License 3.0 available from http://creativecommons.org/licenses/by-sa/3.0/.
Biography
John Goerzen: John Goerzen is an avid programmer, developer for the Debian GNU/Linux project, and a systems administrator. He is the author of several books, including the recent Foundations of Python Network Programming. John is currently the president of Software in the Public Interest, Inc., the legal parent organization of Debian.
- Login or register to post comments
- 28193 reads
- Printer friendly version (unavailable!)




Looking for Linux hosting, reviews, coupons, etc.? See out user-voted list
Best voted contents
-
Is Microsoft trying to kill Apache?
Gary Richmond, 2008-08-08 -
How do Drigg and Pligg compare?
Tony Mobily, 2008-08-17 -
The top 4 internet flame wars about free software
Andrew Min, 2008-08-16 -
Creating wealth with free software
Richard Rothwell, 2008-08-05
Similar entries
Buzz authors
All news
Other sites
- The Top 10 Everything (Dave). The good, the bad and the ugly.
- Free Software news (Dave & Bridget). All about free software -- free as in freedom!
- Book Reviews: Illiterarty (Bridget). Book reviews, blogs, and short stories.
Hot topics - last 60 days
-
Don't compare GNU/Linux with Windows or MacOS - they are not in the same game
Ryan Cartwright, 2008-07-07 -
Self-signed certificates and Firefox 3 - a possible solution
Ryan Cartwright, 2008-08-05 -
Dictators in free and open source software
Tony Mobily, 2008-07-22 -
Why sharing matters more than marketshare to GNU/Linux
Terry Hancock, 2008-08-01 -
Why did Javascript/AJAX mop the floor with Java, Flash and Silverlight? Or, why open standards eventually win
Tony Mobily, 2008-07-30
Hot topics - last 21 days
-
How do Drigg and Pligg compare?
Tony Mobily, 2008-08-17 -
The Bizarre Cathedral - 18
Ryan Cartwright, 2008-08-17 -
Is Microsoft trying to kill Apache?
Gary Richmond, 2008-08-08 -
SliTaz live CD: small but beautifully marked
Gary Richmond, 2008-08-13
Dedicated server