Real World Haskell – Chapter 3

These are the exercises from chapter 3 of
Real World Haskell
by Bryan O’Sullivan, Don Stewart, and John Goerzen

> module RWHChapter3 where

{-# OPTIONS_GHC -XMagicHash #-}

Some useful things to check my work:

> import Test.QuickCheck
> import Data.List
> import GHC.Prim
> import GHC.Base

1) 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.

> lengthList (x:xs) = 1 + lengthList xs
> lengthList [] = 0

check that it works by running

quickCheck (\s -> length s == lengthList s)

at the interactive prompt

> ghclength l = len l 0#
> where
> len :: [a] -> Int# -> Int
> len [] a# = I# a#
> len (_:xs) a# = len xs (a# +# 1#)

is the definition used by GHC.
It doesn’t stack overflow on ghclength [1..1000000]
lengthList [1..1000000] does, at least in ghci

2) Add a type signature for your function to your source file.

> lengthList :: [a] -> Integer

3. 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.)

> listMean x = sum x / fromIntegral (length x)

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

> makePalindrome x = x ++ reverse x

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

> testPalindrome x = x == reverse x

> testPalindrome2 x = (take (halfLength x) x)
> == (take (halfLength x) (reverse x))
> where halfLength x = ((length x) `quot` 2)

reversing the whole list isn’t exactly right

> testPalindrome3 x = (take (halfLength x) x)
> == reverse (drop (halfLength x) x)
> where halfLength x = ((length x) `quot` 2)

except now it thinks that odd length lists aren’t palindromes.

6. 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.)

> sortByLength = sortBy (\a b -> compare (length a) (length b))

7 An intersperse with type intersperse :: a -> [[a]] -> [a], such that
ghci> myintersperse ‘,’ []
“”
ghci> myintersperse ‘,’ [“foo”]
“foo”
ghci> myintersperse ‘,’ [“foo”,”bar”,”baz”,”quux”]
“foo,bar,baz,quux”

> myintersperse :: a -> [[a]] -> [a]
> myintersperse _ [] = []
> myintersperse _ [x] = x
> myintersperse c (x:xs) = x ++ [c] ++ (myintersperse c xs)

8. Height of a tree

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

depth’ :: Tree a -> Int

> depth’ Empty = 0
> depth’ (Node _ b c) = 1 + (max (depth’ b) (depth’ c))

9. 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 either turns left, turns right, or forms a straight line. Define a Direction data type that lets you represent these possibilities.

> data Direction = DLeft
> | DRight
> | DStraight
> deriving (Show)

> data Point = Point {
> x::Double,
> y::Double
> } deriving (Show)

10. Write a function that takes three 2D Points and returns a direction.

> direction p1 p2 p3
> | signCross p1 p2 p3 > | signCross p1 p2 p3 > 0 = DLeft
> | signCross p1 p2 p3 == 0 = DStraight
> where signCross (Point x1 y1) (Point x2 y2) (Point x3 y3) =
> (x2 – x1) * (y3 – y1) – (y2 – y1) * (x3 – x1)

11. Define a function that takes a list of 2D points and computes the direction of each successive triple. Given a list of points [a,b,c,d,e], it should begin by computing the turn made by [a,b,c], then the turn made by [b,c,d], then [c,d,e]. Your function should return a list of Direction.

> directionList :: [Point] -> [Direction]
> directionList ([]) = []
> directionList (_:[]) = []
> directionList (_:_:[]) = []
> directionList (x:x’:x”:[]) = [(direction x x’ x”)]
> directionList (x:x’:x”:xs) = (direction x x’ x”) : directionList (x’:x”:xs)

I don’t particularly like the pattern match on the three x’s. How
about a more general function to do sliding windows of data across the
list?

> window :: Int -> [a] -> [[a]]
> window2 :: Int -> [a] -> [[a]]

> window n xs = if (length xs) > then []
> else (take n xs) : window n (tail xs)

> window2 n xs | (length xs) > window2 n x@(_:xs) = (take n x) : window2 n xs

> direction2 (x1:x2:x3:[]) = direction x1 x2 x3

> directionList2′ (x:xs) = direction2 x : directionList2′ xs
> directionList2′ [] = []

> directionList2 x = directionList2′ (window2 3 x)

I’m still working on the convex hull problem

tabbed working notes



A D A E A
------10-8---------10-8--------10-8--------10-7--------10-8----
----8------10----8------11---8------10---7------10---8------10-
--9-----------10-----------9-----------9-----------9-----------
---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------

Electoral Votes and Poll Closing Times

7:00 PM         Georgia         15
7:00 PM        Indiana        11
7:00 PM        Kentucky 8
7:00 PM        South Carolina 8
7:00 PM         Vermont        3
7:00 PM        Virginia 13
7:30 PM        North Carolina 15
7:30 PM        Ohio        20
7:30 PM        West Virginia 5
8:00 PM        Alabama        9
8:00 PM        Connecticut 7
8:00 PM        Delaware 3
8:00 PM        Florida        27
8:00 PM        Illinois 21
8:00 PM        Maine        4
8:00 PM        Maryland 10
8:00 PM        Massachusetts 12
8:00 PM        Mississippi 6
8:00 PM        Missouri 11
8:00 PM        New Hampshire 4
8:00 PM        New Jersey 15
8:00 PM        Oklahoma 7
8:00 PM        Pennsylvania 21
8:00 PM        Tennessee 11
8:30 PM        Arkansas 6
9:00 PM        Arizona        10
9:00 PM        Colorado 9
9:00 PM        Kansas        6
9:00 PM        Louisiana 9
9:00 PM        Michigan 17
9:00 PM        Minnesota 10
9:00 PM        Nebraska 5
9:00 PM        New Mexico 5
9:00 PM        New York 31
9:00 PM        North Dakota 3
9:00 PM        Rhode Island 4
9:00 PM        South Dakota 3
9:00 PM        Texas        34
9:00 PM        Wisconsin 10
9:00 PM        Wyoming        3
10:00 PM Iowa        7
10:00 PM Montana        3
10:00 PM Nevada        5
10:00 PM Utah        5
11:00 PM California 55
11:00 PM Hawaii        4
11:00 PM Idaho        4
11:00 PM Oregon        7
11:00 PM Washington 11
1:00 AM        Alaska        3

Eeeep

Take a picture of yourself right now.
don’t change your clothes, don’t fix your hair…just take a picture.
post that picture with NO editing.
post these instructions with your picture.

Trying to operatate an iPhone backwards makes me frown.

Eeep….

Take a picture of yourself right now.
don’t change your clothes, don’t fix your hair…just take a picture.
post that picture with NO editing.
post these instructions with your picture.

Camping this last weekend at West Hills

This is the shelter we stayed in. The high pitched roof helps keep the heat in, and the rain out. And the snow, although that mostly melted.


This was the point of the whole trip. A garbage can cooked turkey to share with friends and family. The can acts as an oven, with the charcoal providing way more than enough heat to cook the bird, which is impaled on a stake, holding it vertically in the middle of the can. The 15 pound bird cooked in just a few hours.


My boy Jonathan (on the left), me on the right, and our friend Joel in between. Yes, that’s coffee in the mug. And, yes, it’s a stainless steel vacuum mug. With a spill proof top. But not the one I leave at the office.


My other boy, Michael, against the far wall, near the fire. They’re warming up after we sent them outside to do some of the cooking. Which was mostly making sure there were enough coals around the trash can.
We also made mashed potatoes, sweet potatoes, gravy, peas, corn bread, stuffing, and cherry and apple pies, cooked in dutch ovens. The dutch ovens were in the fireplace opposite the one in this picture, in the far corner of the shelter. Between the two fireplaces, the inside temperature got up into the 80s at one point. We had to open the doors to let out the heat.
And the smoke.

Everyone had a good time, and we came back with the same number of boys we left with.

Office 1 (me 0)

“my office looks like a bookstore exploded in it, and then an electronics store was dropped on it to smother the flames.” – J.Scalzi
http://scalzi.com/whatever/?p=152

The books are all triple stacked, and there are several more shelves surrounding the room….