Haskell Wikibook

Download Haskell Wikibook

Post on 21-Jul-2016

32 views

Category:

Documents

8 download

Embed Size (px)

DESCRIPTION

Haskell Wikibook

TRANSCRIPT

  • Haskell

    en.wikibooks.org

  • April 20, 2014

    On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipediaprojects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. AURI to this license is given in the list of gures on page 589. If this document is a derived workfrom the contents of one of these projects and the content was still licensed by the project underthis license at the time of derivation this document has to be licensed under the same, a similar or acompatible license, as stated in section 4b of the license. The list of contributors is included in chapterContributors on page 585. The licenses GPL, LGPL and GFDL are included in chapter Licenses onpage 593, since this book and/or parts of it may or may not be licensed under one or more of theselicenses, and thus require inclusion of these licenses. The licenses of the gures are given in the list ofgures on page 589. This PDF was generated by the LATEX typesetting software. The LATEX sourcecode is included as an attachment (source.7z.txt) in this PDF le. To extract the source fromthe PDF le, you can use the pdfdetach tool including in the poppler suite, or the http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility. Some PDF viewers may also let you savethe attachment to a le. After extracting it from the PDF le you have to rename it to source.7z.To uncompress the resulting archive we recommend the use of http://www.7-zip.org/. The LATEXsource itself was generated by a program written by Dirk Hnniger, which is freely available underan open source license from http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf.

  • Contents

    1 Haskell Basics 3

    2 Getting set up 52.1 Installing Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2 Very rst steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

    3 Variables and functions 93.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.2 Haskell source les . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.3 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.4 Variables in imperative languages . . . . . . . . . . . . . . . . . . . . . . . 123.5 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.6 Local denitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

    4 Truth values 194.1 Equality and other comparisons . . . . . . . . . . . . . . . . . . . . . . . . 194.2 Boolean values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204.3 Inx operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224.4 Boolean operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234.5 Guards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

    5 Type basics 275.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275.2 Using the interactive :type command . . . . . . . . . . . . . . . . . . . . 285.3 Functional types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305.4 Type signatures in code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

    6 Lists and tuples 396.1 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396.2 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426.3 Retrieving values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446.4 Polymorphic types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

    7 Type basics II 497.1 The Num class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497.2 Numeric types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507.3 Classes beyond numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

    III

  • Contents

    8 Building vocabulary 558.1 Function composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558.2 The need for a vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . 568.3 Prelude and the hierarchical libraries . . . . . . . . . . . . . . . . . . . . . 578.4 One exhibit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578.5 Acquiring vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

    9 Next steps 619.1 if / then / else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619.2 Introducing pattern matching . . . . . . . . . . . . . . . . . . . . . . . . . 629.3 Tuple and list patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659.4 let bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

    10 Simple input and output 6910.1 Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7010.2 Actions under the microscope . . . . . . . . . . . . . . . . . . . . . . . . . 7410.3 Learn more . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

    11 Elementary Haskell 79

    12 Recursion 8112.1 Numeric recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8112.2 List-based recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8612.3 Don't get TOO excited about recursion... . . . . . . . . . . . . . . . . . . 8812.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

    13 More about lists 8913.1 Rebuilding lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8913.2 Generalizing even further . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9113.3 The map function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9313.4 Tips and Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

    14 List processing 9714.1 Folds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9714.2 Scans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10114.3 lter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10214.4 List comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

    15 Type declarations 10715.1 data and constructor functions . . . . . . . . . . . . . . . . . . . . . . . . 10715.2 Deconstructing types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10815.3 type for making type synonyms . . . . . . . . . . . . . . . . . . . . . . . . 110

    16 Pattern matching 11316.1 Analysing pattern matching . . . . . . . . . . . . . . . . . . . . . . . . . . 11316.2 The connection with constructors . . . . . . . . . . . . . . . . . . . . . . . 11416.3 Matching literal values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11616.4 Syntax tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11716.5 Where we can use pattern matching . . . . . . . . . . . . . . . . . . . . . 118

    IV

  • Contents

    17 Control structures 12117.1 if and guards revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12117.2 case expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12217.3 Controlling actions, revisited . . . . . . . . . . . . . . . . . . . . . . . . . . 124

    18 More on functions 12718.1 let and where revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12718.2 Anonymous Functions - lambdas . . . . . . . . . . . . . . . . . . . . . . . 12818.3 Operators and sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

    19 Higher order functions and Currying 13119.1 The Quickest Sorting Algorithm In Town . . . . . . . . . . . . . . . . . . . 13119.2 Now, How Do We Use It? . . . . . . . . . . . . . . . . . . . . . . . . . . . 13219.3 Tweaking What We Already Have . . . . . . . . . . . . . . . . . . . . . . . 13319.4 quickSort, Take Two . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13319.5 But What Did We Gain? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13419.6 Higher-Order Functions and Types . . . . . . . . . . . . . . . . . . . . . . 13419.7 Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13619.8 Function manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

    20 Using GHCi eectively 13920.1 User interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

    21 Intermediate Haskell 141

    22 Modules 14322.1 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14322.2 Importing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14422.3 Exporting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

    23 Indentation 14923.1 The golden rule of indentation . . . . . . . . . . . . . . . . . . . . . . . . . 14923.2 A mechanical translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15123.3 Layout in action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

    24 More on datatypes 15524.1 Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15524.2 Named Fields (Record Syntax) . . . . . . . . . . . . . . . . . . . . . . . . 15524.3 Parameterized Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

    25 Other data structures 16125.1 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16125.2 Other datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

    26 Classes and types 16926.1 Classes and instances . . . . . . . . . . . . . .