SOE ex.10.3



adjust :: [(Color, Region)] -> Coordinate -> (Maybe (Color, Region), [(Color, Region)])
adjust regs p
  = case (break (\(_, r) -> r `containsR` p) regs) of
      (top, hit:rest) -> (Just hit, top ++ rest)
      (_, [])         -> (Nothing, regs)

loop0 :: Window -> [(Color, Region)] -> IO ()
loop0 w regs = do
  clearWindow w
  sequence_ [drawRegionInWindow w c r | (c, r) <- reverse regs]
  (x, y) <- getLBP w
  case (adjust regs (pixelToInch (x - xWin2), pixelToInch (yWin2 - y))) of
    (Nothing, _)        -> closeWindow w
    (Just hit, newRegs) -> loop0 w (hit : newRegs)

loop w regs = do
  clearWindow w
  sequence_ (map (uncurry (drawRegionInWindow w)) (reverse regs))
  (x, y) <- getLBP w
  let aux (_, r) = r `containsR` (pixelToInch (x - xWin2), pixelToInch (yWin2 - y))
  case (break aux regs) of
    (_, [])        -> closeWindow w
    (top, hit:bot) -> loop w (hit:(top++bot))

{-

  Prove loop0 == loop

  because:
       uncurry :: (a -> b -> c) -> (a,b) -> c
  so the 2 blow are the same:
       sequence_ [drawRegionInWindow w c r | (c, r) <- reverse regs]
       sequence_ (map (uncurry (drawRegionInWindow w)) (reverse regs))
  
  and
  the aux function is almost the same as anonymous function 
  inside the case of adjut function with p replace the value pass later.

-}

Leave a Reply