Programming in Haskell

Programming in Haskell - 2nd Edition, [no date]. . Online. Available from: https://web.archive.org/web/20240626213453/https://www.cs.nott.ac.uk/~pszgmh/pih.html [Accessed 15 August 2024].

Slides

Code

Historical Background

1930s:

Alonzo Church develops the Lambda Calculus, a simple but powerful theory of functions.

1950s:

John McCarthy develops Lisp, the first functional language, with some influences from the lambda calculus, but retaining variable assignments.

Lisp allowed Mutation, yet today many confuse functions as first-class citizens with immutability. At the same time, other effects such as exceptions, reflection, communication with the outside world, and concurrency go unmentioned. Adding recursion in the form of feedback-loops to pure combinational circuits lets us implement mutable state via flip-flops. Similarly, using one effect such as concurrency or input/output we can simulate other effects such as mutability.

~

EGUZO, C. V., ROBERT, B. J., IHEMADU, O. C. and AVONG, Patrick Ayuba, 2017. Integrating hardware descriptive language (HDL) in teaching digital electronics-a case of Nigerian polytechnics. In: 2017 IEEE 3rd International Conference on Electro-Technology for National Development (NIGERCON). Online. IEEE. 2017. p. 650–655. Available from: https://ieeexplore.ieee.org/abstract/document/8281935/ [Accessed 17 August 2024].

> The model of fig 1(a) and fig 1(b) shows the behavior of a D flip-flop and shift registers respectively.

~

John Hughes famously stated in his classic paper Why Functional Programming Matters that we cannot make a language more powerful by eliminating features. To that, we add that often we cannot even make a language less powerful by removing features. In this book, Graham demonstrates convincingly that the true value of functional programming lies in leveraging first-class functions to achieve compositionality and equational reasoning. Or in Graham’s own words, “functional programming can be viewed as a style of programming in which the basic method of computation is the application of functions to arguments”. These functions do not necessarily have to be pure or statically typed in order to realise the simplicity, elegance, and conciseness of expression that we get from the functional style.

Gently starting with the IO monad, Graham progresses from functors to applicatives using many concrete examples.

The chapter on monadic parsers brings everything together in a compelling use-case of parsing arithmetic expressions in the implementation of a simple calculator.

This new edition not only adds many more concrete examples of concepts introduced throughout the book, it also introduces the novel Haskell concepts of foldable and traversable types. Readers familiar with object-oriented languages routinely use iterables and visitors to enumerate over all values in a container, or respectively to traverse complex data structures. Haskell’s higher-kinded type classes allow for a very concise and abstract treatment of these concepts by means of the Foldable and Traversable classes. Last but not least, the final chapters of the book give an in-depth overview of lazy evaluation and equational reasoning to prove and derive programs. The capstone chapter on calculating compilers especially appeals to me because it touches a topic that has had my (Erik Meijer) keen interest for many decades, ever since my own PhD thesis on the same topic.

While there are plenty of alternative textbooks on Haskell in particular and functional programming in general, Graham’s book is unique amongst all of these in that it uses Haskell simply as a Tool for Thought, and never attempts to sell Haskell or functional programming as a silver bullet that magically solves all programming problems. It focuses on elegant and concise expression of intent and thus makes a strong case of how pure and lazy functional programming is an intelligible medium for efficiently reasoning about algorithms at a high level of abstraction. The skills you acquire by studying this book will make you a much better programmer no matter what language you use to actually program in. In the past decade, using the first edition of this book I have taught many tens of thousands of students how to juggle with code. With this new edition, I am looking forward to extending this streak for at least another 10 years.