Haskell

A very different language

Download the whole article as PDF

Short URL: http://fsmsh.com/1160

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
Don't miss out on the other pages!
1234next ›last »

Write a full post in response to this!

0

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.



CariNet: Cloud computing is a reality.