Solutions to RWH Exercises

Chapter 1 Exercises

  1. Integer, Integer, Integer, Integer, Double, Integer, Integer, Interger, Integer, Double, Integer, Integer, Integer, Integer, Integer
  2. I see all values bound to variables
  3. Based on WC.hs in the chapter:
    -- file: ch01/WC_ex3.hs
    -- Notes:
    --  the words function splits a list of characters on a space
    main = interact wordCount
        where wordCount input = show (length (words(input))) ++ "\n"
  4. Based on WC.hs in the chapter:
    -- file: ch01/WC_ex2.hs
    -- Notes:
    --  a string is just a list of characters
    main = interact wordCount
        where wordCount input = show (length input) ++ "\n"

 


Chapter 2 Mid-chapter Exercises

 

  1. Bool;  ([[Char]],Char) which is a tuple containing a list of strings and a character; [(Bool, [[Char]])] which is a list of tuples containing a bool and a list of strings

Chapter 2 End-of-chapter Exercises

  1. Return an element from a list; return a list
  2. --file: lastButOne.hs (Exercise 2)
    lastButOne xs = if length (xs) > 2
                    then lastButOne(tail(xs))
                    else head(xs)
  3. If the list length is greater than one, the above solution works.  If the list has one element, it incorrectly returns the only element in the list.  If the list is empty, an exception is thrown:
    *** Exception: Prelude.head: empty list

 


Chapter 3 Mid-chapter Exercises

 

  1. fromList (Cons x xs) = x : fromList xs
    fromList Nil         = []
  2. data Tree a = Node a (Maybe (Tree a)) (Maybe (Tree a))

Chapter 3 End-of-chapter Exercises

  1. --file: ch03/ex1.hs
    numElem (x:xs) = 1 + numElem(xs)
    numElem []     = 0
  2. --file: ch03/ex2.hs
    numElem :: [a] -> Int
    numElem (x:xs) = 1 + numElem(xs)
    numElem []     = 0
  3. --file: ch03/ex3.hs
    computeMean x = sum (x) / fromIntegral( length x)
  4. --file: ch03/ex4.hs
    palindrome :: [a] -> [a]
    palindrome (x:xs)  = [x] ++ palindrome xs ++ [x]
    palindrome []      = []
  5. It is not pretty, but it works. The “Eq a=>” addition to the type line was required to get the == operator work. As of writing this solution, I do not know what this does…
    --file: ch03/ex5.hs
    ispalindrome :: Eq a => [a] -> Bool
    ispalindrome [] = True
    ispalindrome x  = if sizeSafe && firstElem == lastElem && ispalindrome midElem
                      then True
                      else False
          where sizeSafe  = if length x `mod` 2 == 0
                            then True
                            else False
                firstElem = head x
                lastElem  = head( reverse ( tail x ) )
                midElem   = drop 1 (reverse (drop 1 (reverse x)))

 


Chapter 4 Mid-chapter Exercises

 

  1. import Data.Maybe
    
    safeHead :: [a] -> Maybe a
    safeHead [] = Nothing
    safeHead xs = Just (head xs)
    
    safeTail :: [a] -> Maybe [a]
    safeTail [] = Nothing
    safeTail xs = Just (tail xs)
    
    safeLast :: [a] -> Maybe a
    safeLast [] = Nothing
    safeLast xs = Just (last xs)
    
    safeInit :: [a] -> Maybe [a]
    safeInit []     = Nothing
    safeInit (x:[]) = Just []
    safeInit (x:xs) = Just (x : init xs)
    

Leave a Comment