Skip to content

Instantly share code, notes, and snippets.

@njbartlett
Created August 4, 2010 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njbartlett/507783 to your computer and use it in GitHub Desktop.
Save njbartlett/507783 to your computer and use it in GitHub Desktop.
countCrossings :: TestCase -> Int
countCrossings = length . guardedPairs crosses
-- This is the "pairs" function from the first meeting, with the addition of a
-- predicate function to filter out unwanted pairs
guardedPairs :: (a -> a -> Bool) -> [a] -> [(a,a)]
guardedPairs p xs = [(x,y) | (x:ys) <- tails xs, y <- ys, p x y]
crosses :: Wire -> Wire -> Bool
crosses (Wire s1 e1) (Wire s2 e2) = if s1 < s2 then e1 > e2 else e1 < e2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment