Online Book Reader

Home Category

Learn You a Haskell for Great Good! - Miran Lipovaca [1]

By Root 433 0
the result of the function. Haskell, being a lazy language, takes advantage of this fact and defers actually computing results for as long as possible. Once you want your results to be displayed, Haskell will do just the bare minimum computation required to display them. Laziness also allows you to make seemingly infinite data structures, because only the parts of the data structures that you choose to display will actually be computed.

Let’s look at an example of Haskell’s laziness. Say you have a list of numbers, xs = [1,2,3,4,5,6,7,8], and a function called doubleMe that doubles every element and returns the result as a new list. If you want to multiply your list by 8, your code might look something like this:

doubleMe(doubleMe(doubleMe(xs)))

An imperative language would probably pass through the list once, make a copy, and then return it. It would then pass through the list another two times, making copies each time, and return the result.

In a lazy language, calling doubleMe on a list without forcing it to show you the result just makes the program tell you, “Yeah yeah, I’ll do it later!” Once you want to see the result, the first doubleMe calls the second one and says it wants the result immediately. Then the second one says the same thing to the third one, and the third one reluctantly gives back a doubled 1, which is 2. The second doubleMe receives that and returns 4 to the first one. The first doubleMe then doubles this result and tells you that the first element in the final resulting list is 8. Because of Haskell’s laziness, the doubleMe calls pass through the list just once, and only when you really need that to happen.

Haskell is statically typed. This means that when you compile your program, the compiler knows which piece of code is a number, which is a string, and so on. Static typing means that a lot of possible errors can be caught at compile time. If you try to add together a number and a string, for example, the compiler will whine at you.

Haskell uses a very good type system that has type inference. This means that you don’t need to explicitly label every piece of code with a type, because Haskell’s type system can intelligently figure it out. For example, if you say a = 5 + 4, you don’t need to tell Haskell that a is a number—it can figure that out by itself. Type inference makes it easier for you to write code that’s more general. If you write a function that takes two parameters and adds them together, but you don’t explicitly state their type, the function will work on any two parameters that act like numbers.

Haskell is elegant and concise. Because it uses a lot of high-level concepts, Haskell programs are usually shorter than their imperative equivalents. Shorter programs are easier to maintain and have fewer bugs.

Haskell was made by some really smart guys (with PhDs). Work on Haskell began in 1987 when a committee of researchers got together to design a kick-ass language. The Haskell Report, which defines a stable version of the language, was published in 1999.

What You Need to Dive In

In short, to get started with Haskell, you need a text editor and a Haskell compiler. You probably already have your favorite text editor installed, so we won’t waste time on that. The most popular Haskell compiler is the Glasgow Haskell Compiler (GHC), which we will be using throughout this book.

The best way to get what you need is to download the Haskell Platform. The Haskell Platform includes not only the GHC compiler but also a bunch of useful Haskell libraries! To get the Haskell Platform for your system, go to http://hackage.haskell.org/platform/ and follow the instructions for your operating system.

GHC can compile Haskell scripts (usually with an .hs extension), and it also has an interactive mode. From there, you can load functions from scripts and then call them directly to see immediate results. Especially when you’re learning, it’s much easier to use the interactive mode than it is to compile and run your code every time you make a change.

Once you’ve installed the Haskell Platform,

Return Main Page Previous Page Next Page

®Online Book Reader