haskell notes

Download Haskell Notes

Post on 08-Oct-2015

12 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

hn

TRANSCRIPT

  • Notes on Functional Programming with Haskell

    H. Conrad Cunninghamcunningham@cs.olemiss.edu

    Multiparadigm Software Architecture GroupDepartment of Computer and Information Science

    University of Mississippi201 Weir Hall

    University, Mississippi 38677 USA

    Fall Semester 2014

  • Copyright c 1994, 1995, 1997, 2003, 2007, 2010, 2014 by H. Conrad Cunningham

    Permission to copy and use this document for educational or research purposes ofa non-commercial nature is hereby granted provided that this copyright notice isretained on all copies. All other rights are reserved by the author.

    H. Conrad Cunningham, D.Sc.Professor and ChairDepartment of Computer and Information ScienceUniversity of Mississippi201 Weir HallUniversity, Mississippi 38677USA

    cunningham@cs.olemiss.edu

  • PREFACE TO 1995 EDITION

    I wrote this set of lecture notes for use in the course Functional Programming (CSCI555) that I teach in the Department of Computer and Information Science at the Uni-versity of Mississippi. The course is open to advanced undergraduates and beginninggraduate students.

    The first version of these notes were written as a part of my preparation for the fallsemester 1993 offering of the course. This version reflects some restructuring andrevision done for the fall 1994 offering of the courseor after completion of the class.For these classes, I used the following resources:

    Textbook Richard Bird and Philip Wadler. Introduction to Functional Program-ming, Prentice Hall International, 1988 [2].

    These notes more or less cover the material from chapters 1 through 6 plusselected material from chapters 7 through 9.

    Software Gofer interpreter version 2.30 (2.28 in 1993) written by Mark P. Jones,available via anonymous FTP from directory pub/haskell/gofer at the Inter-net site nebula.cs.yale.edu.

    Gofer is an interpreter for a dialect of the lazy functional programming lan-guage Haskell. This interpreter was available on both MS-DOS-based PC-compatibles, 486-based systems executing FreeBSD (UNIX), and other UNIXsystems.

    Manual Mark P. Jones. An Introduction to Gofer (Version 2.20), tutorial manualdistributed as a part of the Gofer system [15].

    In addition to the Bird and Wadler textbook and the Gofer manual, I used thefollowing sources in the preparation of these lecture notes:

    Paul Hudak and Joseph H. Fasel. A Gentle Introduction to Haskell, ACMSIGPLAN NOTICES, Vol. 27, No. 5, May 1992 [12].

    Paul Hudak, Simon Peyton Jones, and Philip Wadler. Report on the Pro-gramming Language Haskell: A Non-strict, Purely Functional Language, ACMSIGPLAN NOTICES, Vol. 27, No. 5, May 1992 [13].

    E. P. Wentworth. Introduction to Functional Programming using RUFL, De-partment of Computer Science, Rhodes University, Grahamstown, South Africa,August 1990 [22].

    This is a good tutorial and manual for the Rhodes University Functional Lan-guage (RUFL), a Haskell-like language developed by Wentworth. I used RUFLfor two previous offerings of my functional programming course, but switched to

    iii

  • Gofer for the fall semester 1993 offering. My use this source was indirectviamy handwritten lecture notes for the previous versions of the class.

    Paul Hudak. Conception, Evolution, and Application of Functional Program-ming Languages, ACM Computing Surveys , Vol. 21, No. 3, pages 359411,September 1989 [11].

    Rob Hoogerwoord. The Design of Functional Programs: A Calculational Ap-proach, Doctoral Dissertation, Eindhoven Technical University, Eindhoven, TheNetherlands, 1989 [10].

    A. J. T. Davie An Introduction to Functional Programming Systems UsingHaskell, Cambridge University Press, 1992 [7].

    Anthony J. Field and Peter G. Harrison. Functional Programming, AddisonWesley, 1988 [8].

    This book uses the eager functional language Hope.

    J. Hughes. Why Functional Programming Matters, The Computer Journal,Vol. 32, No. 2, pages 98107, 1989 [14].

    Although the Bird and Wadler textbook is excellent, I decided to supplement thebook with these notes for several reasons:

    I wanted to use Gofer/Haskell language concepts, terminology, and exampleprograms in my class presentations and homework exercises. Although closeto Haskell, the language in Bird and Wadler differs from Gofer and Haskellsomewhat in both syntax and semantics.

    Unlike the stated audience of the Bird and Wadler textbook, my students usu-ally have several years of experience in programming using traditional languageslike Pascal, C, or Fortran. This is both an advantage and a disadvantage. Onthe one hand, they have programming experience and programming languagefamiliarity on which I can build. On the other hand, they have an imperativemindset that sometimes is resistant to the declarative programming approach.I tried to take both into account as I drafted these notes.

    Because of a change in the language used from RUFL to Gofer, I needed torewrite my lecture notes in 1993 anyway. Thus I decided to invest a bit moreeffort and make them available in this form. (I expected about 25% more effort,but it probably took about 100% more effort. :-)

    The publisher of the Bird and Wadler textbook told me a few weeks before my1993 class began that the book would not be available until halfway throughthe semester. Fortunately, the books arrived much earlier than predicted. Inthe future, I hope that these notes will give me a backup should the book notbe available when I need it.

    iv

  • Overall, I was reasonably satisfied with the 1993 draft of the notes. However, I didnot achieve all that I wanted. Unfortunately, other obligations did not allow meto substantially address these issues in the current revision. I hope to address thefollowing shortcomings in any future revision of the notes.

    I originally wanted the notes to introduce formal program proof and synthesisconcepts earlier and in a more integrated way than these notes currently do.But I did not have sufficient time to reorganize the course and develop the newmaterials needed. Also the desire to give nontrivial programming exercises ledme to focus on the language concepts and features and informal programmingtechniques during the first half of the course.

    Gofer/Haskell is a relatively large language with many features. In 1993 I spentmore time covering the language features than I initially planned to do. In the1994 class I reordered a few of the topics, but still spent more time on languagefeatures. For future classes I need to rethink the choice and ordering of thelanguage features presented. Perhaps a few of the language features should beomitted in an introductory course.

    Still yet there are a few important features that I did not cover. In particular, Idid not discuss the more sophisticated features of the type system in any detail(e.g., type classes, instances, and overloading).

    I did not cover all the material that I have in covered in one or both of theprevious versions of the course (e.g., cyclic structures, abstract data types, theeight queens problem, and applications of trees).

    1997 Note: The 1997 revision is limited to the correction of a few errors. The springsemester 1997 class is using the new Hugs interpreter rather than Gofer and the text-book Haskell: The Craft of Functional Programming by Simon Thompson (Addison-Wesley, 1996).

    2014 Note: The 2014 revision seeks primarily to update these Notes to use Haskell2010 and the Haskell Platform (i.e., GHC and GHCi). The focus is on chapters3, 5, 6, 7, 8, and 10, which are being used in teaching a Haskell-based functionalprogramming module in CSci 450 (Organization of Programming Languages).

    v

  • Acknowledgements

    I thank the many students in the CSCI 555 classes who helped me find many typo-graphical and presentation errors in the working drafts of these notes. I also thankthose individuals at other institutions who have examined these notes and suggestedimprovements.

    I thank Diana Cunningham, my wife, for being patient with all the late nights ofwork that writing these notes required.

    The preparation of this document was supported by the National Science Foundationunder Grant CCR-9210342 and by the Department of Computer and InformationScience at the University of Mississippi.

    vi

  • Contents

    1 INTRODUCTION 1

    1.1 Course Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

    1.2 Excerpts from Backus 1977 Turing Award Address . . . . . . . . . . 2

    1.3 Programming Language Paradigms . . . . . . . . . . . . . . . . . . . 5

    1.4 Reasons for Studying Functional Programming . . . . . . . . . . . . . 6

    1.5 Objections Raised Against Functional Programming . . . . . . . . . . 11

    2 FUNCTIONS AND THEIR DEFINITIONS 13

    2.1 Mathematical Concepts and Terminology . . . . . . . . . . . . . . . . 13

    2.2 Function Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

    2.3 Mathematical Induction over Natural Numbers . . . . . . . . . . . . 15

    3 FIRST LOOK AT HASKELL 17

    4 USING THE INTERPRETER 23

    5 HASKELL BASICS 25

    5.1 Built-in Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

    5.2 Programming with List Patterns . . . . . . . . . . . . . . . . . . . . . 30

    5.2.1 Summation of a list (sumlist) . . . . . . . . . . . . . . . . . . 30

    5.2.2 Length of a list (length) . . . . . . . . . . . . . . . . . . . . 32

    5.2.3 Removing adjacent duplicates (remdups) . . . . . . . . . . . . 32

    5.2.4 More example patterns . . . . . . . . . . . . . . . . . . . . . . 34

    5.3 Infix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

    5.4 Recursive Programming Styles . . . . . . . . . . . . . . . . . . . . . . 36

    5.4.1 Append