Chapter 4: Basic Datatypes

This note captures my solutions to exercises in Chapter 4: Basic Datatypes of the book Haskell programming from first principles

Exercises: Mood swing

  1. The type constructor is Mood.
  2. If the function requires a Mood value, we can only use Blah or Woot.
  3. What is wrong with changeMood :: Mood -> Woot is that Woot is not a type constructor. It is a data constructor. A data constructor cannot appear in the type signature, it can only appear at the term level.
  4. Mood should not appear at the term level since it is a type constructor. Fixed function is shown below.
module ChangeMood where

    data Mood = Blah | Woot deriving Show

    changeMood :: Mood -> Mood
    changeMood Blah = Woot
    changeMood    _ = Blah

changeMood.hs

Exercises: Find the mistakes

  1. not True && true should be not True && True
  2. not (x = 6) should be not (x == 6). This will not compile because x is not defined.
  3. (1 * 2) > 5. This works without any fix.
  4. [Merry] > [Happy] should be ["Merry"] > ["Happy"].
  5. [1, 2, 3] ++ "look at me!" should be ['1','2','3'] ++ "look at me!".

Chapter exercises

awesome = ["Papuchon", "curry", ":)"]
alsoAwesome = ["Quake", "The Simons"]
allAwesome = [awesome, alsoAwesome]

Question 1

length has a type signature [a] -> Int. It takes a list of any type. It evaluates to a type of Int.

Question 2

  1. length [1, 2, 3, 4, 5] gives 5.
  2. length [(1, 2), (2, 3), (3, 4)] gives 3. This is a list of tuples.
  3. length allAwesome gives 2. allAwesome is a list of two values: awesome and alsoAwesome.
  4. length (concat allAwesome) gives 5. concat flattens allAwesome and produces a list of 5 strings.

Question 3

6 / 3 works. This produces 2.0. 6 / length [1, 2, 3] returns an error because length returns an Int datatype whereas the operator (/) belongs to a Fractional typeclass which has no instance for the Int datatype. Int belongs to the Integral typeclass instead.

Question 4

We can use div function. Alternatively, we can use fromIntegral to coerce the Integral typeclass to Num typeclass. fromIntegral has a type (Integral a, Num b) => a -> b.

-- #4
-- Using a different operator
ghci> 6 `div` length [1, 2, 3]
2
-- Alternatively
ghci> 6 / fromIntegral (length [1, 2, 3])
2.0

Question 5

The type of the expression 2 + 3 == 5 is Bool. The expected result is True.

Question 6

  1. The type of x in x = 5 is Num a => a. The expected result is 5.
  2. The type of x + 3 == 5 is Bool. The expected result is False if x = 5.

Question 7

  1. length allAwesome == 2 gives True. This works because we are comparing two values of the same datatype Int.
  2. length [1, 'a', 3, 'b'] does not work because a list cannot contain values of different datatypes.
  3. length allAwesome + length awesome gives 5. This works because we are adding values of the same datatype Int.
  4. (8 == 8) && ('b' < 'a') gives False. This works because we are performing a boolean And operation on two values of the Bool datatype. (8 == 8) is True and ('b' < 'a') is False, therefore True and False gives False.
  5. (8 == 8) && 9 does not work because we are trying to compare Bool with a numeric datatype.

Question 8

module IsPalindrome where
    isPalindrome :: (Eq a) => [a] -> Bool
    isPalindrome x = x == reverse x

isPalindrome.hs

Question 9

module MyAbs where
    myAbs :: Integer -> Integer 
    myAbs x = if x >= 0 then x else -x

myAbs.hs

Question 10

f :: (a, b) -> (c, d) -> ((b, d), (a, c))
f x y = (,) (snd x, snd y) (fst x, fst y)

f.hs

Correcting syntax

module CorrectingSyntax where
    -- #1
    x :: Int -> Int -> Int
    x = (+)
    
    f :: Foldable t => t a -> Int
    f xs = w `x` 1 where 
        w = length xs
    
    -- #2
    f' :: p -> p
    f' x = x

    -- #3
    f'' :: (a, b) -> a
    f'' (a, b) = a

correctingSyntax.hs

Match the function names to their types

  1. The type of show is Show a => a -> String (option c).
  2. The type of (==) is Eq a => a -> a -> Bool (option b).
  3. The type of fst is (a, b) -> a (option a).
  4. The type of (+) is (+) :: Num a => a -> a -> a (option d).