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
- 26455 reads
- Printer friendly version (unavailable!)




Best voted contents
-
Free software heroes: from Stallman to Google, a list of inspiring individuals who made everything possible
Tony Mobily, 2008-06-15 -
Ian Lynch's take on the BECTA fiasco
Tony Mobily, 2008-06-17 -
The Groklaw effect hits Becta. And yes, I am coining a new term
Tony Mobily, 2008-06-15 -
Mail merge in OpenOffice.org
Michael Crider, 2008-06-17
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
-
A future without Microsoft
Tony Mobily, 2008-06-08 -
Vienna failed to migrate to GNU/Linux: why?
Tony Mobily, 2008-06-09 -
Free software heroes: from Stallman to Google, a list of inspiring individuals who made everything possible
Tony Mobily, 2008-06-15 -
Dubious ads in Free Software Magazine
Tony Mobily, 2008-05-25 -
The Bizarre Cathedral - 6
Ryan Cartwright, 2008-05-25
Hot topics - last 21 days
-
Free software heroes: from Stallman to Google, a list of inspiring individuals who made everything possible
Tony Mobily, 2008-06-15 -
The Groklaw effect hits Becta. And yes, I am coining a new term
Tony Mobily, 2008-06-15 -
Is Asus backsliding on GNU/Linux?
Gary Richmond, 2008-06-19 -
Ian Lynch's take on the BECTA fiasco
Tony Mobily, 2008-06-17
Dedicated server