copied from http://www.cs.nott.ac.uk/~gmh//pearl.pdf