public
Created

  • Download Gist
Ropes.hs
Haskell
1 2 3 4 5 6 7 8 9 10
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.