module Main where import Text.Printf (printf) data ExtInt = NegInfty | Normal Int | PosInfty deriving (Show, Eq, Ord) adjacentZipper :: (a -> a -> Bool) -> [a] -> ([a], [a]) adjacentZipper cmp items = go [] items where go front [] = (front, []) go [] (x:rest) = go [x] rest go ys@(y:_) xs@(x:rest) = if cmp x y then go (x:ys) rest else (ys, xs) almostIncreasing :: [Int] -> Bool almostIncreasing nums = check ([NegInfty] ++ map Normal nums ++ [PosInfty]) where check items = case adjacentZipper (>) items of (_, []) -> True ((x2:x1:_), right@(y1:y2:_)) -> (x1 < y1 || x2 < y2) && sorted right sorted = null . snd . adjacentZipper (>) main :: IO () main = do printCase [] printCase [0, 1] printCase [1, 0] printCase [5, 1, 2, 3] printCase [1, 2, 0, 3] printCase [1, 5, 2, 3] printCase [1, 5, 0, 2] printCase [3, 2, 1, 0] where printCase xs = printf "%s => %s\n" (show xs) (show $ almostIncreasing xs)