What Comes To Mind
Thursday, October 15, 2009
  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 < 0 = DRight
> | 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) < n
> then []
> else (take n xs) : window n (tail xs)

> window2 n xs | (length xs) < n = []
> 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
 

Wednesday, September 30, 2009
  Testing embedded TeX
If I set the up correctly, I can now embed mathematical formulas in my blog:


\int_{0}^{1}\frac{x^{4}\left(1-x\right)^{4}}{1+x^{2}}dx
=\frac{22}{7}-\pi
 

Thursday, July 02, 2009
  oops.
 

Thursday, April 30, 2009
  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-----------
---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------
 

Tuesday, November 04, 2008
  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

 

Thursday, September 18, 2008
  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.

 

Thursday, January 31, 2008
  email check
 

Random notes and stuff

My Photo
Name: Steve Downey
Archives
September 2004 / January 2005 / February 2005 / March 2005 / May 2005 / June 2005 / October 2005 / January 2006 / February 2006 / March 2006 / January 2007 / February 2007 / April 2007 / November 2007 / December 2007 / January 2008 / September 2008 / November 2008 / April 2009 / July 2009 / September 2009 / October 2009 /


Powered by Blogger

Subscribe to
Posts [Atom]