haskell写题求大佬指导讨论改进
查看原帖
haskell写题求大佬指导讨论改进
625398
_OTZ_楼主2024/10/1 10:59

想请大佬们看看有没有可以改进的写法思路

module Main where

import Control.Monad (replicateM)
import Data.Maybe (fromMaybe)
import Data.Monoid (Last (..))

main :: IO ()
main = do
  carpetCount <- read <$> getLine
  carpets <- replicateM carpetCount (fmap read . words <$> getLine)
  [targetPointx, targetPointy] <- fmap read . words <$> getLine
  putStrLn . show . (fromMaybe $ negate 1) . getLast $
    foldr1 (<>) ((checkPointInBounds (targetPointx, targetPointy)) <$> (zip [1 ..] carpets))

type Point = (Int, Int)

type PointBounds = [Int]

type IndexedPointBounds = (Int, [Int])

checkPointInBounds :: Point -> IndexedPointBounds -> Last Int
checkPointInBounds (x, y) (idx, [bottomLeftx, bottomLefty, width, height]) =
  if x >= bottomLeftx
    && x <= bottomLeftx + width
    && y >= bottomLefty
    && y <= bottomLefty + height
    then Last . Just $ idx
    else Last $ Nothing
checkPointInBounds _ _ = undefined
2024/10/1 10:59
加载中...