Write Yourself a Scheme in 48 Hours

From Wikibooks, open books for an open world
Jump to: navigation, search


Write Yourself a Scheme in 48 Hours
List of Topics





Write Yourself a Scheme in 48 Hours


This is an import of the tutorial, Write Yourself a Scheme in 48 Hours, by Jonathan Tang. Thanks to Jonathan for his permission on this import. Note to contributors, please feel free to modify this wikified version of the tutorial!


Most Haskell tutorials on the web seem to take a language-reference-manual approach to teaching. They show you the syntax of the language, a few language constructs, and then have you construct a few simple functions at the interactive prompt. The "hard stuff" of how to write a functioning, useful program is left to the end, or sometimes omitted entirely.

This tutorial takes a different tack. You'll start off with command-line arguments and parsing, and progress to writing a fully-functional Scheme interpreter that implements a good-sized subset of R5RS Scheme. Along the way, you'll learn Haskell's I/O, mutable state, dynamic typing, error handling, and parsing features. By the time you finish, you should be fairly fluent in both Haskell and Scheme.

There are two main audiences targeted by this tutorial:

  1. People who already know Lisp or Scheme and want to learn Haskell
  2. People who don't know any programming language, but have a strong quantitative background and are familiar with computers

The second group will likely find this challenging, as I gloss over several Scheme and general programming concepts to stay focused on the Haskell. A good textbook like Structure and Interpretation of Computer Programs or The Little Schemer may help a lot here.

Users of a procedural or object-oriented language like C, Java, or Python should beware, however: You'll have to forget most of what you already know about programming. Haskell is completely different from those languages, and requires a different way of thinking about programming. It's best to go into this tutorial with a blank slate and try not to compare Haskell to imperative languages, because many concepts in them (classes, functions, return) have a significantly different meaning in Haskell.

Since each lesson builds on the code written for the previous one, it's probably best to go through the lessons in order.

This tutorial assumes that you'll be using GHC as your Haskell compiler. It may work with e.g. Hugs, but it hasn't been tested at all, and you may need to download additional libraries.

The source files are available here: listings.


  1. First Steps: Compiling and running
  2. Parsing
  3. Evaluation, Part 1
  4. Error Checking and Exceptions
  5. Evaluation, Part 2
  6. Building a REPL: Basic I/O
  7. Adding Variables and Assignment: Mutable State in Haskell
  8. Defining Scheme Functions: Closures and Environments
  9. Creating IO Primitives: File I/O
  10. Towards a Standard Library: Fold and Unfold
  11. Conclusion & Further Resources
  12. Answers to Exercises