fork(1) download
  1. module Main where
  2.  
  3. import Text.Printf (printf)
  4.  
  5. data ExtInt = NegInfty | Normal Int | PosInfty deriving (Show, Eq, Ord)
  6.  
  7. adjacentZipper :: (a -> a -> Bool) -> [a] -> ([a], [a])
  8. adjacentZipper cmp items = go [] items
  9. where go front [] = (front, [])
  10. go [] (x:rest) = go [x] rest
  11. go ys@(y:_) xs@(x:rest) = if cmp x y then go (x:ys) rest else (ys, xs)
  12.  
  13. almostIncreasing :: [Int] -> Bool
  14. almostIncreasing nums = check ([NegInfty] ++ map Normal nums ++ [PosInfty])
  15. where check items = case adjacentZipper (>) items of
  16. (_, []) -> True
  17. ((x2:x1:_), right@(y1:y2:_)) -> (x1 < y1 || x2 < y2) && sorted right
  18. sorted = null . snd . adjacentZipper (>)
  19.  
  20. main :: IO ()
  21. main = do
  22. printCase []
  23. printCase [0, 1]
  24. printCase [1, 0]
  25. printCase [5, 1, 2, 3]
  26. printCase [1, 2, 0, 3]
  27. printCase [1, 5, 2, 3]
  28. printCase [1, 5, 0, 2]
  29. printCase [3, 2, 1, 0]
  30. where printCase xs = printf "%s => %s\n" (show xs) (show $ almostIncreasing xs)
Success #stdin #stdout 0s 4476KB
stdin
Standard input is empty
stdout
[] => True
[0,1] => True
[1,0] => True
[5,1,2,3] => True
[1,2,0,3] => True
[1,5,2,3] => True
[1,5,0,2] => False
[3,2,1,0] => False