Thinking Functionally with Haskell Read Online Free

Thinking Functionally with Haskell
Pages:
Go to
calculator is available as a Windows system called WinGHCi. If you open this window, you will get something like GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done.
    Loading package integer-gmp ... linking ... done.
    Loading package base ... linking ... done.
    Prelude>
    The prompt Prelude> means that the standard library of prelude functions, predeclared types and other values is loaded. You can now use GHCi as a supercalculator: Prelude> 3^5
    243
    Prelude> import Data.Char
    Prelude Data.Char> map toLower "HELLO WORLD!"
    "hello world!"
    Prelude Data.Char>
    The function toLower resides in the library Data.Char . After importing this library you have access to the functions defined in the library. Note that the prompt changes and now indicates the libraries that have been loaded. Such prompts can grow in size very quickly. But we can always change the prompt: Prelude> :set prompt ghci>
    ghci>
    For brevity we will use this prompt throughout the book.
    You can load a script, Numbers2Words.lhs say, that contains the definition of convert as follows: ghci> :load "Numbers2Words.lhs"
    [1 of 1] Compiling Main ( Numbers2Words.lhs, interpreted )
    Ok, modules loaded: Main.
    ghci>
    We will explain what modules are in the next chapter. Now you can type, for example, ghci> convert 301123
    "three hundred and one thousand one hundred and twenty-three" ghci> We end the chapter with some exercises. These contain additional points of interestand should be regarded as an integral part of the text. The same is true for all subsequent chapters, so please read the questions even if you do not answer them. The answers are given afterwards.
1.6 Exercises
    Exercise A
    Consider the function
    double :: Integer -> Integer
    double x = 2*x
    that doubles an integer. What are the values of the following expressions?
    map double [1,4,4,3]
    map (double . double) [1,4,4,3]
    map double []
    Suppose sum :: [Integer] -> Integer is a function that sums a list of integers. Which of the following assertions are true and why?
    sum . map double = double . sum
    sum . map sum = sum . concat
    sum . sort = sum
    You will need to recall what the function concat does. The function sort sorts a list of numbers into ascending order.
    Exercise B
    In Haskell, functional application takes precedence over every other operator, so double 3+4 means (double 3)+4 , not double (3+4) . Which of the following expressions is a rendering of sin 2 θ into Haskell?
    sin^2 theta sin theta^2 (sin theta)^2
    (Exponentiation is denoted by (^) .) How would you express sin 2 θ /2 π as a wellformed Haskell expression?
    Exercise C
    As we said in the text, a character, i.e. an element of Char , is denoted using single quotes, and a string is denoted using double quotes. In particular the string "Hello World!" is just a much shorter way of writing the list ['H','e','l','l','o',' ','W','o','r','l','d','!']
    General lists can be written with brackets and commas. (By the way, parentheses are round, brackets are square, and braces are curly.) The expressions 'H' and "H" therefore have different types. What are they? What is the difference between 2001 and "2001" ?
    The operation ++ concatenates two lists. Simplify
    [1,2,3] ++ [3,2,1]
    "Hello" ++ " World!"
    [1,2,3] ++ []
    "Hello" ++ "" ++ "World!"
    Exercise D
    In the common words example we started off by converting every letter in the text to lowercase, and then we computed the words in the text. An alternative is to do things the other way round, first computing the words and then converting each letter in each word to lowercase. The first method is expressed by words . map toLower . Give a similar expression for the second method.
    Exercise E
    An operator ⊕ is said to be associative if x ⊕ ( y ⊕ z ) = ( x ⊕ y ) ⊕ z . Is numerical addition associative? Is list concatenation associative? Is functional composition associative? Give an example of an operator on numbers that is not
Go to

Readers choose