Daniel Keast

Real World Haskell 2

haskell, programming

There are several more questions in this book at the end of the chapter.

Write a function that computes the number of elements in a list. To test it, ensure that it gives the same answers as the standard length function.

len (x:xs) = 1 + len xs
len []     = 0

Add a type signature for your function to your source file. To test it, load the source file into ghci again.

len :: Num a => [b] -> a

Write a function that computes the mean of a list, i.e., the sum of all elements in the list divided by its length. (You may need to use the fromIntegral function to convert the length of the list from an integer into a floating-point number.)

mean :: (Integral a, Fractional b) => [a] -> b
mean [] = 0.0
mean xs = fromIntegral (my_sum xs) / len xs
        where my_sum []     = 0
              my_sum (x:xs) = x + my_sum xs

Turn a list into a palindrome; i.e., it should read the same both backward and forward. For example, given the list [1,2,3], your function should return [1,2,3,3,2,1].

rev :: [a] -> [a]
rev [] = []
rev (x:xs) = xs ++ [x]

palindrome :: [a] -> [a]
palindrome xs = xs ++ rev xs

Write a function that determines whether its input list is a palindrome.

isPalindrome :: Eq a => [a] -> Bool
isPalindrome xs = xs == rev xs

Create a function that sorts a list of lists based on the length of each sublist. (You may want to look at the sortBy function from the Data.List module.)

import Data.List

lenSort :: [[a]] -> [[a]]
lenSort xs = sortBy orderByLen xs
           where orderByLen a b
                  | aLen > bLen = GT
                  | aLen < bLen = LT
                  | otherwise   = EQ
                  where aLen = len a
                        bLen = len b

Define a function that joins a list of lists together using a separator value:

myIntersperse :: a -> [[a]] -> [a]
myIntersperse _   []     = []
myIntersperse _   (x:[]) = x
myIntersperse sep (x:xs) = x ++ [sep] ++ myIntersperse sep xs

Using the binary tree type that we defined earlier in this chapter, write a function that will determine the height of the tree. The height is the largest number of hops from the root to an Empty. For example, the tree Empty has height zero; Node “x” Empty Empty has height one; Node “x” Empty (Node “y” Empty Empty) has height two; and so on.

data Tree a = Node a (Tree a) (Tree a)
            | Empty
              deriving (Show)

height :: (Ord b, Num b) => Tree a -> b
height Empty        = 0
height (Node _ a b) = if left > right
                      then left
                      else right
                    where left  = 1 + height a
                          right = 1 + height b

Consider three two-dimensional points, a, b, and c. If we look at the angle formed by the line segment from a to b and the line segment from b to c, it turns left, turns right, or forms a straight line. Define a Direction data type that lets you represent these possibilities.

data Direction = LeftTurn
               | RightTurn
               | Straight
                 deriving (Show, Eq)

There are three more questions after these. I have answers for two of them, but I dont’t think they’re right… I’ll hopefully be able to get them done.