Notes on Functional Programming with Haskell
Total Page:16
File Type:pdf, Size:1020Kb
Notes on Functional Programming with Haskell H. Conrad Cunningham [email protected] Multiparadigm Software Architecture Group Department of Computer and Information Science University of Mississippi 201 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 of a non-commercial nature is hereby granted provided that this copyright notice is retained on all copies. All other rights are reserved by the author. H. Conrad Cunningham, D.Sc. Professor and Chair Department of Computer and Information Science University of Mississippi 201 Weir Hall University, Mississippi 38677 USA [email protected] PREFACE TO 1995 EDITION I wrote this set of lecture notes for use in the course Functional Programming (CSCI 555) 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 beginning graduate students. The first version of these notes were written as a part of my preparation for the fall semester 1993 offering of the course. This version reflects some restructuring and revision done for the fall 1994 offering of the course|or 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 plus selected 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 UNIX systems. Manual { Mark P. Jones. An Introduction to Gofer (Version 2.20), tutorial manual distributed as a part of the Gofer system [15]. In addition to the Bird and Wadler textbook and the Gofer manual, I used the following sources in the preparation of these lecture notes: • Paul Hudak and Joseph H. Fasel. \A Gentle Introduction to Haskell", ACM SIGPLAN 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", ACM SIGPLAN 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 RUFL for two previous offerings of my functional programming course, but switched to iii Gofer for the fall semester 1993 offering. My use this source was indirect|via my 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 359{411, September 1989 [11]. • Rob Hoogerwoord. The Design of Functional Programs: A Calculational Ap- proach, Doctoral Dissertation, Eindhoven Technical University, Eindhoven, The Netherlands, 1989 [10]. • A. J. T. Davie An Introduction to Functional Programming Systems Using Haskell, Cambridge University Press, 1992 [7]. • Anthony J. Field and Peter G. Harrison. Functional Programming, Addison Wesley, 1988 [8]. This book uses the \eager" functional language Hope. • J. Hughes. \Why Functional Programming Matters," The Computer Journal, Vol. 32, No. 2, pages 98{107, 1989 [14]. Although the Bird and Wadler textbook is excellent, I decided to supplement the book with these notes for several reasons: • I wanted to use Gofer/Haskell language concepts, terminology, and example programs in my class presentations and homework exercises. Although close to Haskell, the language in Bird and Wadler differs from Gofer and Haskell somewhat 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 languages like Pascal, C, or Fortran. This is both an advantage and a disadvantage. On the one hand, they have programming experience and programming language familiarity on which I can build. On the other hand, they have an imperative mindset 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 to rewrite my lecture notes in 1993 anyway. Thus I decided to invest a bit more effort 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 my 1993 class began that the book would not be available until halfway through the semester. Fortunately, the books arrived much earlier than predicted. In the future, I hope that these notes will give me a \backup" should the book not be available when I need it. iv Overall, I was reasonably satisfied with the 1993 draft of the notes. However, I did not achieve all that I wanted. Unfortunately, other obligations did not allow me to substantially address these issues in the current revision. I hope to address the following shortcomings in any future revision of the notes. • I originally wanted the notes to introduce formal program proof and synthesis concepts 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 new materials needed. Also the desire to give nontrivial programming exercises led me to focus on the language concepts and features and informal programming techniques during the first half of the course. • Gofer/Haskell is a relatively large language with many features. In 1993 I spent more time covering the language features than I initially planned to do. In the 1994 class I reordered a few of the topics, but still spent more time on language features. For future classes I need to rethink the choice and ordering of the language features presented. Perhaps a few of the language features should be omitted in an introductory course. • Still yet there are a few important features that I did not cover. In particular, I did 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 the previous versions of the course (e.g., cyclic structures, abstract data types, the eight queens problem, and applications of trees). 1997 Note: The 1997 revision is limited to the correction of a few errors. The spring semester 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 Haskell 2010 and the Haskell Platform (i.e., GHC and GHCi). The focus is on chapters 3, 5, 6, 7, 8, and 10, which are being used in teaching a Haskell-based functional programming 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 thank those individuals at other institutions who have examined these notes and suggested improvements. I thank Diana Cunningham, my wife, for being patient with all the late nights of work that writing these notes required. The preparation of this document was supported by the National Science Foundation under Grant CCR-9210342 and by the Department of Computer and Information Science 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 Appending lists (++) ....................... 36 5.4.2 Reversing a list (rev)....................... 37 5.4.3 Recursion Terminology . 38 5.4.4 Tail recursive reverse (reverse') . 39 vii 5.4.5 Local definitions (let and where) . 40 5.4.6 Fibonacci numbers . 41 5.5 More List Operations . 43 5.5.1 Element selection (!!)...................... 43 5.5.2 List-breaking operations (take and drop) . 43 5.5.3 List-combining operations (zip) . 44 5.6 Rational Arithmetic Package . 44 5.7 Exercises . 48 6 HIGHER-ORDER FUNCTIONS 55 6.1 Maps . 55 6.2 Filters . 57 6.3 Folds . 58 6.4 Strictness . 61 6.5 Currying and Partial Application . 62 6.6 Operator Sections . 64 6.7 Combinators . 65 6.8 Functional Composition . 66 6.9 Lambda Expressions .