Haskell Tutorial for C Programmers - Introduction

Written by Eric Etheridge
version 2.0
last updated May 18, 2009
year of original release: 2005

Table of Contents

Each major section has its own page.
  1. Introduction
    1. Abstract
    2. Downloads
    3. Purpose and Other Online References
    4. Relationship to Our Other Tutorials
    5. Preface and Style Notes

  2. Section I: What the Heck is Going On?
    1. Part I: Haskell's Oddity
    2. Part II: Input and Output
    3. Part III: Very Basic Intro to Types
    4. Part IV: Haskell's Lists and List Comprehensions
    5. Part V: Making Sense of 'fibs', and Why Lazy Evaluation is Important

  3. Section II: Towards Functions
    1. Part I: The Order of Operations as a Programmer
    2. Part II: Functions, But Really a Sidetrack to Types
    3. Part III: More Types, Because Haskell Is 'Polymorphic'
    4. Part IV: Functions Already

  4. Section III: Now Let's Really Write Functions
    1. Part I: Did You Take That Break? Here Are Patterns
    2. Part II: After Patterns, Guards
    3. Part III: 'If'
    4. Part IV: Indention Syntax
    5. Part V: And Lambda Functions
    6. Part VI: Polymorphic Types and Type Constructors
    7. Part VII: The IO Monad
    8. Part VIII: Dissecting the IO Example

  5. Section IV: Haskell and You
    1. Part I: Where Are the 'For' Loops?
    2. Part II: Remember Lazy Evaluation? It's Still There
    3. Part III: The Point(s)

  6. Section V: Final Commentary
    1. Part I: Why is 'Referential Transparency' Worth Anything?
    2. Part II: Feedback and Notes
    3. Part III: Ranting

  7. Section VI: Extended Examples
    1. Part I: Intro to Examples
    2. Part II: Calculating Pi
    3. Part III: MergeSort Variations
    4. Part IV: Regular Expressions and Finite Automata: Overview
    5. Part V: Regular Expressions and Finite Automata: Types
    6. Part VI: Regular Expressions and Finite Automata: String to RE, and Monads
    7. Part VII: Regular Expressions and Finite Automata: Conversion and Execution
    8. Part VIII: Solving 2CNFs in Linear Time
    9. Part IX: In Closing

Back to our main page.

Introduction

Abstract


Many people are accustomed to imperative languagues, which include C, C++, Java, Python, and Pascal. For computer science students, Haskell is weird and obtuse. This tutorial assumes that the reader is familiar with C/C++, Python, Java, or Pascal. I am writing for you because it seems that no other tutorial was written to help students overcome the difficulty of moving from C/C++, Java, and the like to Haskell.

I write this assuming that you have checked out the Gentle Introduction to Haskell, but still don't understand what's going on.

Haskell is not 'a little different,' and will not 'take a little time.' It is very different and you cannot simply pick it up, although I hope that this tutorial will help.

I am going to put many pauses in this tutorial because learning Haskell hurt a lot, at least for me. I needed breaks, and my brain hurt while I was trying to understand.

Haskell has both more flexibility and more control than most languages. Nothing that I know of beats C's control, but Haskell has everything C does unless you need to control specific bytes in memory. So I call Haskell powerful, rather than just 'good.'

I wrote this tutorial because Haskell was very hard for me to learn, but now I love it. "Haskell is hard!" "You can't write code the way I know how!" "My brain hurts!" "There aren't any good references!" That's what I said when I was in college. There were good references, but they didn't cover the real problem: coders know C.

This abstract was pieced together by Mark Evans, here, from my own work. I have had no contact with Mark Evans, but since he did't contact me when he editted together this abstract from my work and posted it on lambda-the-ultimate, I doubt he'll care that I've taken that edit and used it as my abstract here. If he wishes, he may contact me regarding the legal status of this work. For now, I assume I still hold the copyright on all of it, including the abstract (but see the creative commons license below).

 

Downloads


Here is the html of this tutorial, zipped using bzip2: bzip2 of html, 34K, and zipped as a zip: zip of html, 51K.

Here are the source files and text for all examples for this tutorial, including all those in the sections and the large examples at the end, zipped using bzip2: bzip2 of sources, 28K, and zipped as a zip: zip of sources, 43K.

If you don't have bzip2, you can get the latest version at www.bzip.org.

Sources for the in-text examples in the coming sections are given in the following files:

  1. ExampleSectionsCode.hs
  2. ExampleSectionsTry.txt

 

License


I've decided that I should be specific about the license for this tutorial. To sum up, you can do whatever you want with this tutorial as long as my name is still on it, including modifying it, redistributing it, or selling derivative works. Specifically, you can use it to educate people in a commercial setting, such as in-house training or consulting. I would love to hear that a company considering Haskell used this tutorial to train its workforce. So feel free. Any derivative works also carry this license, by the way. "Share alike". Thank you, Creative Commons. The link leads to the legal wording. This license has been updated to a newer version, but it is basically identical to the original.

Creative Commons License
Haskell Tutorial for C Programmers by Eric Etherige is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.

 

This Tutorial's Purpose and Other Online References


Many people are accustomed to imperative languagues, which include C, C++, Java, Python, and Pascal. In fact, most languages in common usage are imperative, other than LISP, Scheme, ML, and OCaml. For computer science students in high school or early college, it is virtually guaranteed that Haskell is weird and obtuse. I first encountered Haskell in the classroom when I was a freshman at UT Austin, and then in another class at UT two years later. I was only familiar with C/C++, Pascal, and QBASIC, and all of the Haskell tutorials and books seemed to assume more of my education. This tutorial assumes that the reader is familiar with C/C++, Python, Java, or Pascal. This tutorial is specifically for students of computer science, people in their first few years of college, or even in high school. I am writing for you because it seems that no other tutorial was written to help students overcome the difficulty of moving from C/C++, Java, and the like to Haskell.

New: To learn and use Haskell, you should install GHC, and perhaps Hugs. GHC is the "de facto standard" compiler for Haskell, and almost all projects in Haskell use it. The Hugs interpreter is a simpler tool that will let you play around and learn. Start with Hugs if you are having trouble using GHC. GHC also ships with GHCi, "GHC interactive", which is a command line interpreter much like Hugs, but no GUI. Getting these programs is easy. If you use Debian, GHC and Hugs are packages. For everyone else, the homepages are here:

http://www.haskell.org/ghc/

http://www.haskell.org/hugs/

I write this assuming that you have checked out the following tutorial, the Gentle Introduction to Haskell, but found that you still don't understand what's going on:

http://www.haskell.org/tutorial/

The Gentle Introduction to Haskell is a good reference for basic syntax. In this tutorial we will skip most syntax details until later. First we will cover defining functions in Haskell and why it is central to the language. For more syntax details, here is another tutorial, the Tour of the Haskell Syntax, which has much more specific information:

http://cs.anu.edu.au/Student/comp1100/haskell/tourofsyntax.html

You should look through the Tour, since it describes the appropriate syntax for most of the things I discuss. The Tour is useful because you can understand it without knowing everything about Haskell. Reading these can help you before, after, or during this tutorial.

One of the best references is the source code for the Prelude, which is the file "Prelude.hs". This file holds the code for all of the general-purpose functions in the Prelude module. If any function shows up that you don't understand, you can look up its definition in source code and figure out what it's really doing. This is a very good practice for those unfamiliar with general Haskell use.

There are (at least) three ways to get a copy of the Prelude.hs file. If you download and install Hugs, Prelude.hs will be in the libraries directory. I do not think that GHC ships with the uncompiled library sources. You can download a source version of GHC from its download page. You may be able to find a gzipped copy in the ghc "libsrc" Debian package.

Another important resource is the GHC Hierarchical Libraries documentation. The data types and functions of every module are defined here, including the Prelude. Whenever you use a library function, you'll want to refer to these to find the module and specific usage. All the standard modules are well documented. If your GHC installation includes the docs, these webpages are also on your local machine.

http://www.haskell.org/ghc/docs/latest/html/libraries/index.html

 

Relationship to Our Other Tutorials


You may be here because you're trying to use HOpenGL. The tutorial you're reading was written along with our other tutorial on rendering and texture examples in HOpenGL, and if you are familiar with Haskell you can go directly there:

Dave Morra's HOpenGL tutorial

Otherwise, read this tutorial and those mentioned above first, then check out our HOpenGL tutorial if you like. If we've written well enough you should be able to not only use Haskell but HOpenGL as well. On with the tutorial.

This tutorial was originally published in 2005. There are added examples and corrections of links and example code.

 

Preface and Style Notes


I am not writing a Haskell reference. This is a tutorial designed to take someone having trouble understanding Haskell and help them. This tutorial is for people who, like me, needed to learn enough concepts to understand the code covered in a classroom. Haskell allows things to be done easily and clearly, but it is not easy or clear, and it can be extremely challenging for a novice. You cannot pick up Haskell code and understand it. What I have attempted to write is a tutorial that covers the common aspects of Haskell that are the most obtuse.

As the tutorial progresses, one thing should become clear about Haskell: its real power comes into play when you attack difficult problems. Because of this, I use some difficult problems in this tutorial. Don't worry if you don't understand the solutions after reading the tutorial once. Haskell is not a toy language, and even a moderately sized set of functions will include several of Haskell's complicated tools all working together. This has left educators with a dilemma: do I use ridiculously simple code in order to cover a single topic at once, or do I use something actually useful and try to explain all the pieces and how they all fit together? Many tutorials and lessons have chosen the former, but I prefer the latter. That means that each example requires a lot of explaining. Often concepts must be explained once in extremely simplistic terms, and then explained again later after other related topics have also been briefly discussed. As you read this tutorial, remember this: Haskell's real power is the fact that all of its pieces fit so well together, not just that they are good pieces.

The syntax and variable name conventions I use in this tutorial are those used in the Haskell source code and libraries, and what I learned in college. Haskell programs tend to be short, but wide. I recommend using descriptive variable names, even for indices and so forth.

 

Continue to I: What the Heck is Going On?