Haskell: The Craft of Functional Programming (3rd Edition) (International Computer Science Series)
Format: PDF / Kindle (mobi) / ePub
Introducing functional programming in the Haskell language, this book is written for students and programmers with little or no experience. It emphasises the process of crafting programmes, problem solving and avoiding common programming pitfalls.
Covering basic functional programming, through abstraction to larger scale programming, students are lead step by step through the basics, before being introduced to more advanced topics.
This edition includes new material on testing and domain-specific languages and a variety of new examples and case studies, including simple games. Existing material has been expanded and re-ordered, so that some concepts – such as simple data types and input/output – are presented at an earlier stage.
function or other object refers to the object itself. This section concentrates on explaining the idea o l recursion, and why it makes sense. In particular we give two complementary explanations of how primitive recursion works in defining the factorial function over the natural numbers. In the section after this we look at how recursion is u w d in practice. Recursion 59 Getting started: a story about factorials Suppose that someone tells us that the factorial of a natural number is the
containing guards, we should supply data for each case in the definition. We should also pay attention to 'boundary conditions' by testing the equality case when a guard uses >= or >, for example. If a function uses recursion we should test the zero case, the one case and the general case. I n the example of mysteryMax we should be guided to the data 6 6 2 since the first two inputs are at the boundaries of the guards We take up the ideas discussed in this section when we discuss proof in
The shunt function moves the elements from one list onto another, thus shunt :: [a] -> [a] -> [a] shunt 11 ys shunt (x:xs) ys = = ys shunt xs (x:ys) Starting with an empty second argument, we have (shunt.1) (shunt.2 ) Generalizing the proof goal 149 and so we can reverse lists using this function: r e v : : Cal -> [a1 r e v xs = shunt x s [I ( r e v . 1) Now we turn to looking at properties of the r e v function. First proof attempt Reversing a list twice should give us back the
Section 10.9 below. We have already seen other direct definitions, as when we said : : P i c t u r e -> P i c t u r e flipH = reverse flipH We note that this definition has exactly the same effect as saying flipH p i c = reverse pic since if we were to use (f l i p H . I ) applied to the picture horse, say, the first step of the evaluation would be the step flipH horse reverse horse 2, in which f lipH gets replaced by r e v e r s e . Function composition Figure 10.1 169 Function
signifies function composition. in which the output of onc function becomes the input of another. I n pictures, we see the creation of a new function by connecting together the input and output of two given functions: obviously this suggests inany other ways of connecting together functions, many of which we will look at in the chapters to come. The direct combination of functions gives us the first example of the power of functional programming: we are able to combine functions using an