Thinking Functionally with Haskell Read Online Free Page B

Thinking Functionally with Haskell
Pages:
Go to
concatenation and functional composition are all associative. But of course, numerical subtraction isn’t. Nor is exponentiation. The identity element of addition is 0, the identity element of concatenation is the empty list, and the identity element of functional composition is the identity function:
    id :: a -> a
    id x = x
    Answer to Exercise F
    This exercise follows Section 1.3 quite closely. One way of computing the function anagrams n is as follows: 1. Extract the words of length n , using a function
    getWords :: Int -> [Word] -> [Word]
    2.Take each word and add a label to it. The label consists of the characters of the word, sorted into alphabetical order. For example, word is turned into the pair ("dorw","word") This labelling is achieved by the function addLabel :: Word -> (Label,Word)
    where
    type Label = [Char]
    3. Sort the list of labelled words into alphabetical order of label, using the function sortLabels :: [(Label,Word)] -> [(Label,Word)]
    4. Replace each group of adjacent labelled words with the same label with a single entry consisting of a pair in which the first component is the common label and the second component is a list of words with that label. This uses a function groupByLabel :: [(Label,Word)] -> [(Label,[Word])]
    5. Replace each entry by a string using a function
    showEntry :: [(Label,[Word])] -> String
    and concatenate the results.
    That gives
    anagrams n = concat . map showEntry . groupByLabel .
    sortLabels . map addLabel . getWords n
    Answer to Exercise G
    One possible solution:
    song n = if n==0 then ""
    else song (n-1) ++ "\n" ++ verse n
    verse n = line1 n ++ line2 n ++ line3 n ++ line4 n
     
    line1 n = if n==1 then
    "One man went to mow\n"
    else
    numbers!!(n-2) ++ " men went to mow\n"
    line2 n = "Went to mow a meadow\n"
    line3 n = if n==1 then
    "One man and his dog\n"
    else
    numbers!!(n-2) ++ " men, " ++ count (n-2) ++ "one man and his dog\n"
    line4 n = "Went to mow a meadow\n\n"
     
    count n = if n==0 then ""
    else
    numbs!!(n-1) ++ " men, " ++ count (n-1)
     
    numbers = ["Two", "Three", "Four", "Five", "Six",
    "Seven", "Eight", "Nine"]
    numbs = ["two", "three", "four", "five", "six",
    "seven", "eight"]
    Notice that we have omitted to declare the types of the component functions and values in this script. Although Haskell will infer the correct types, it is usually a good idea to put them in for all functions and other values, however simple the types may be. Scripts with explicit type signatures are clearer to read and provide a useful check on the validity of definitions.
1.8 Chapter notes
    If you are interested in the origins of Haskell, you should definitely read The History of Haskell , a copy of which is obtainable at research.microsoft.com/~simonpj/papers/history-of-haskell
    One of the abiding strengths of Haskell is that it wasn’t designed to be a closed language, and researchers were encouraged to implement novel programming ideas and techniques by building language extensions or libraries. Consequently, Haskell is a large language and there are numerous books, tutorials and papers devoted to various aspects of the subject, including the recent Parallel and Concurrent Programming in Haskell by Simon Marlow (O’Reilly, 2013). Pointers to much of the material can be found at www.haskell.org . But three books in particular were open on my desk while writing this text. The first is Haskell 98, Languages and Libraries, The Revised Report (Cambridge University Press, 2003), edited by Simon Peyton Jones. This is an indispensable aid in understanding the nitty-gritty of the first standard version of Haskell, called Haskell 98. An online version of the report is available at www.haskell.org/onlinereport
    The present book mostly follows this standard, though it does not cover the whole language by any means.
    Since then a new standard, Haskell 2010, has been released; see
    haskell.org/onlinereport/haskell2010/
    One change is that module names are now hierarchical, so we write Data.List

Readers choose

Christine Rimmer

L. P. Hartley

Beverly Barton

N.C. Reed

E. J. Swift

Tim O'Rourke

Rhea Regale

Rodger Moffet, Amanda Moffet, Donald Cuthill, Tom Moss