PEARLS OF FUNCTIONAL ALGORITHM DESIGN

Similar documents
Conducting the Reference Interview:

THE PROMOTION OF SOCIAL AWARENESS

Diagnostic Test. Middle School Mathematics

Advanced Grammar in Use

Lecture Notes on Mathematical Olympiad Courses

International Series in Operations Research & Management Science

South Carolina English Language Arts

Guide to Teaching Computer Science

Introduction to Moodle

Paper 2. Mathematics test. Calculator allowed. First name. Last name. School KEY STAGE TIER

Mathematics. Mathematics

Extending Place Value with Whole Numbers to 1,000,000

International Examinations. IGCSE English as a Second Language Teacher s book. Second edition Peter Lucantoni and Lydia Kellas

Instrumentation, Control & Automation Staffing. Maintenance Benchmarking Study

Written by Wendy Osterman

music downloads. free and free music downloads like

Presentation Advice for your Professional Review

Interactive Whiteboard

Backwards Numbers: A Study of Place Value. Catherine Perez

1 st Quarter (September, October, November) August/September Strand Topic Standard Notes Reading for Literature

Intermediate Algebra

PROFESSIONAL TREATMENT OF TEACHERS AND STUDENT ACADEMIC ACHIEVEMENT. James B. Chapman. Dissertation submitted to the Faculty of the Virginia

A Metacognitive Approach to Support Heuristic Solution of Mathematical Problems

COMMUNICATION-BASED SYSTEMS

A Practical Introduction to Teacher Training in ELT

FREE COLLEGE Can Happen to You!

US and Cross-National Policies, Practices, and Preparation

Course Syllabus for Math

HDR Presentation of Thesis Procedures pro-030 Version: 2.01

Learning Microsoft Publisher , (Weixel et al)

Perspectives of Information Systems

The Good Judgment Project: A large scale test of different methods of combining expert predictions

CLASSIFICATION OF TEXT DOCUMENTS USING INTEGER REPRESENTATION AND REGRESSION: AN INTEGRATED APPROACH

GACE Computer Science Assessment Test at a Glance

Strategies for Solving Fraction Tasks and Their Link to Algebraic Thinking

Writing the Personal Statement

Minutes. Student Learning Outcomes Committee March 3, :30 p.m. Room 2411A

Classroom Connections Examining the Intersection of the Standards for Mathematical Content and the Standards for Mathematical Practice

University of Groningen. Systemen, planning, netwerken Bosman, Aart

Honors Mathematics. Introduction and Definition of Honors Mathematics

CS Machine Learning

Developing Grammar in Context

AUTONOMY. in the Law

JEFFERSON COLLEGE COURSE SYLLABUS BUS 261 BUSINESS COMMUNICATIONS. 3 Credit Hours. Prepared by: Cindy Rossi January 25, 2014

Enter Samuel E. Braden.! Tenth President

Beyond PDF. Using Wordpress to create dynamic, multimedia library publications. Library Technology Conference, 2016 Kate McCready Shane Nackerud

Curriculum Vitae FARES FRAIJ, Ph.D. Lecturer

Function Tables With The Magic Function Machine

Automating Outcome Based Assessment

TESL/TESOL Certification

Lecture Notes in Artificial Intelligence 4343

Syllabus Fall 2014 Earth Science 130: Introduction to Oceanography

THE HEAD START CHILD OUTCOMES FRAMEWORK

Page 1 of 11. Curriculum Map: Grade 4 Math Course: Math 4 Sub-topic: General. Grade(s): None specified

Learn & Grow. Lead & Show

Mathematics subject curriculum

Mathematics process categories

Bittinger, M. L., Ellenbogen, D. J., & Johnson, B. L. (2012). Prealgebra (6th ed.). Boston, MA: Addison-Wesley.

Reviewed by Florina Erbeli

VOCATIONAL QUALIFICATION IN YOUTH AND LEISURE INSTRUCTION 2009

EDEXCEL FUNCTIONAL SKILLS PILOT. Maths Level 2. Chapter 7. Working with probability

Characteristics of the Text Genre Realistic fi ction Text Structure

Algebra 1, Quarter 3, Unit 3.1. Line of Best Fit. Overview

SUMMARY REPORT MONROE COUNTY, OH OFFICIAL RESULTS PRIMARY ELECTION MARCH 6, 2012 RUN DATE:03/20/12 11:03 AM STATISTICS REPORT-EL45 PAGE 001

A Teacher Toolbox. Let the Great World Spin. for. by Colum McCann ~~~~ The KCC Reads Selection. for the. Academic Year ~~~~

1 Use complex features of a word processing application to a given brief. 2 Create a complex document. 3 Collaborate on a complex document.

How to make an A in Physics 101/102. Submitted by students who earned an A in PHYS 101 and PHYS 102.

Availability of Grants Largely Offset Tuition Increases for Low-Income Students, U.S. Report Says

Software Security: Integrating Secure Software Engineering in Graduate Computer Science Curriculum

Statewide Framework Document for:

Mcgraw Hill 2nd Grade Math

Foothill College Summer 2016

A Minimalist Approach to Code-Switching. In the field of linguistics, the topic of bilingualism is a broad one. There are many

TabletClass Math Geometry Course Guidebook

Concept Acquisition Without Representation William Dylan Sabo

Excel Formulas & Functions

Syllabus ENGR 190 Introductory Calculus (QR)

Descriptive Summary of Beginning Postsecondary Students Two Years After Entry

SOUTHERN MAINE COMMUNITY COLLEGE South Portland, Maine 04106

THEORETICAL CONSIDERATIONS

School of Innovative Technologies and Engineering

BENCHMARK MA.8.A.6.1. Reporting Category

Athens: City And Empire Students Book (Cambridge School Classics Project) By Cambridge School Classics Project

Process to Identify Minimum Passing Criteria and Objective Evidence in Support of ABET EC2000 Criteria Fulfillment

Inside the mind of a learner

Math 098 Intermediate Algebra Spring 2018

Bitstrips for Schools: A How-To Guide

Focused on Understanding and Fluency

Division Strategies: Partial Quotients. Fold-Up & Practice Resource for. Students, Parents. and Teachers

OFFICE SUPPORT SPECIALIST Technical Diploma

Problem Solving for Success Handbook. Solve the Problem Sustain the Solution Celebrate Success

Communication and Cybernetics 17

Missouri Mathematics Grade-Level Expectations

Designing a case study

Administrative Master Syllabus

BENG Simulation Modeling of Biological Systems. BENG 5613 Syllabus: Page 1 of 9. SPECIAL NOTE No. 1:

A R "! I,,, !~ii ii! A ow ' r.-ii ' i ' JA' V5, 9. MiN, ;

First Grade Curriculum Highlights: In alignment with the Common Core Standards

MGMT 3280: Strategic Management

Snow Falling On Cedars By David Guterson

Transcription:

PEARLS OF FUNCTIONAL ALGORITHM DESIGN In Pearls of Functional Algorithm Design takes a radically new approach to algorithm design, namely design by calculation. The body of the text is divided into 30 short chapters, called pearls, each of which deals with a particular programming problem. These problems, some of which are completely new, are drawn from sources as diverse as games and puzzles, intriguing combinatorial tasks and more familiar algorithms in areas such as data compression and string matching. Each pearl starts with the statement of the problem expressed using the functional programming language Haskell, a powerful yet succinct language for capturing algorithmic ideas clearly and simply. The novel aspect of the book is that each solution is calculated from the problem statement by appealing to the laws of functional programming. Pearls of Functional Algorithm Design will appeal to the aspiring functional programmer, students and teachers interested in the principles of algorithm design, and anyone seeking to master the techniques of reasoning about programs in an equational style. RICHARD BIRD is Professor of Computer Science at Oxford University and Fellow of Lincoln College, Oxford.

PEARLS OF FUNCTIONAL ALGORITHM DESIGN RICHARD BIRD University of Oxford

University Printing House, Cambridgei CB2i8 BS,iUnited Kingdom Cambridge University Press is part of the University of Cambridge. It furthers the University s mission by disseminating knowledge in the pursuit of education, learning and research at the highest international levels of excellence. Information on this title: /9780521513388 c Cambridge University Press 2010 This publication is in copyright. Subject to statutory exception and to the provisions of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press. First published 2010 7th printing 2014 Printed in the United Kingdom by Clays, St Ives plc. A catalogue record for this publication is available from the British Library Library of Congress Cataloging-in-Publication Data Bird, Richard, 1943 Pearls of functional algorithm design /. p. cm. ISBN 978-0-521-51338-8 (Hardback) 1. Functional programming (Computer science) 2. Computer algorithms. I. Title. QA76.62.B57 2010 006.3 1 dc22 2010022871 ISBN 978-0-521-51338-8 Hardback Cambridge University Press has no responsibility for the persistence or accuracy of URLs for external or third-party internet websites referred to in this publication, and does not guarantee that any content on such websites is, or will remain, accurate or appropriate.

Dedicated to my wife, Norma.

Contents Preface page ix 1 The smallest free number 1 2 A surpassing problem 7 3 Improving on saddleback search 12 4 A selection problem 21 5 Sorting pairwise sums 27 6 Making a century 33 7 Building a tree with minimum height 41 8 Unravelling greedy algorithms 50 9 Finding celebrities 56 10 Removing duplicates 64 11 Not the maximum segment sum 73 12 Ranking suffixes 79 13 The Burrows Wheeler transform 91 14 The last tail 102 15 All the common prefixes 112 16 The Boyer Moore algorithm 117 17 The Knuth Morris Pratt algorithm 127 18 Planning solves the Rush Hour problem 136 19 A simple Sudoku solver 147 20 The Countdown problem 156 21 Hylomorphisms and nexuses 168 22 Three ways of computing determinants 180 23 Inside the convex hull 188 vii

viii Contents 24 Rational arithmetic coding 198 25 Integer arithmetic coding 208 26 The Schorr Waite algorithm 221 27 Orderly insertion 231 28 Loopless functional algorithms 242 29 The Johnson Trotter algorithm 251 30 Spider spinning for dummies 258 Index 275

Preface In 1990, when the Journal of Functional Programming (JFP) was in the stages of being planned, I was asked by the then editors, Simon Peyton Jones and Philip Wadler, to contribute a regular column to be called Functional Pearls. The idea they had in mind was to emulate the very successful series of essays that Jon Bentley had written in the 1980s under the title Programming Pearls in the Communications of the ACM. Bentley wrote about his pearls: Just as natural pearls grow from grains of sand that have irritated oysters, these programming pearls have grown from real problems that have irritated programmers. The programs are fun, and they teach important programming techniques and fundamental design principles. I think the editors had asked me because I was interested in the specific task of taking a clear but inefficient functional program, a program that acted as a specification of the problem in hand, and using equational reasoning to calculate a more efficient one. One factor that stimulated growing interest in functional languages in the 1990s was that such languages were good for equational reasoning. Indeed, the functional language GOFER, invented by Mark Jones, captured this thought as an acronym. GOFER was one of the languages that contributed to the development of Haskell, the language on which this book is based. Equational reasoning dominates everything in this book. In the past 20 years, some 80 pearls have appeared in the JFP, together with a sprinkling of pearls at conferences such as the International Conference of Functional Programming (ICFP) and the Mathematics of Program Construction Conference (MPC). I have written about a quarter of them, but most have been written by others. The topics of these pearls include interesting program calculations, novel data structures and small but elegant domain-specific languages embedded in Haskell and ML for particular applications. ix

x Preface My interest has always been in algorithms and their design. Hence the title of this book is Pearls of Functional Algorithm Design rather than the more general Functional Pearls. Many, though by no means all, of the pearls start with a specification in Haskell and then go on to calculate a more efficient version. My aim in writing these particular pearls is to see to what extent algorithm design can be cast in a familiar mathematical tradition of calculating a result by using well-established mathematical principles, theorems and laws. While it is generally true in mathematics that calculations are designed to simplify complicated things, in algorithm design it is usually the other way around: simple but inefficient programs are transformed into more efficient versions that can be completely opaque. It is not the final program that is the pearl; rather it is the calculation that yields it. Other pearls, some of which contain very few calculations, are devoted to trying to give simple explanations of some interesting and subtle algorithms. Explaining the ideas behind an algorithm is so much easier in a functional style than in a procedural one: the constituent functions can be more easily separated, they are brief and they capture powerful patterns of computation. The pearls in this book that have appeared before in the JFP and other places have been polished and repolished. In fact, many do not bear much resemblance to the original. Even so, they could easily be polished more. The gold standard for beauty in mathematics is Proofs from The Book by Aigner and Ziegler (third edition, Springer, 2003), which contains some perfect proofs for mathematical theorems. I have always had this book in mind as an ideal towards which to strive. About a third of the pearls are new. With some exceptions, clearly indicated, the pearls can be read in any order, though the chapters have been arranged to some extent in themes, such as divide and conquer, greedy algorithms, exhaustive search and so on. There is some repetition of material in the pearls, mostly concerning the properties of the library functions that we use, as well as more general laws, such as the fusion laws of various folds. A brief index has been included to guide the reader when necessary. Finally, many people have contributed to the material. Indeed, several pearls were originally composed in collaboration with other authors. I would like to thank Sharon Curtis, Jeremy Gibbons, Ralf Hinze, Geraint Jones and Shin-Cheng Mu, my co-authors on these pearls, for their kind generosity in allowing me to rework the material. Jeremy Gibbons read the final draft and made numerous useful suggestions for improving the presentation. Some pearls have also been subject to close scrutiny at meetings of the Algebra of Programming research group at Oxford. While a number of flaws and errors have been removed, no doubt additional ones have been introduced. Apart

Preface xi from those mentioned above, I would like to thank Stephen Drape, Tom Harper, Daniel James, Jeffrey Lake, Meng Wang and Nicholas Wu for many positive suggestions for improving presentation. I would also like to thank Lambert Meertens and Oege de Moor for much fruitful collaboration over the years. Finally, I am indebted to David Tranah, my editor at Cambridge University Press, for encouragement and support, including much needed technical advice in the preparation of the final copy.