Skip to content

Instantly share code, notes, and snippets.

@chessai
Created November 1, 2018 18:40
Show Gist options
  • Save chessai/f20deb39b0a1a83949201cc6c2ab94b6 to your computer and use it in GitHub Desktop.
Save chessai/f20deb39b0a1a83949201cc6c2ab94b6 to your computer and use it in GitHub Desktop.
data Ordered a = Empty | Decreasing a | Increasing a
inc :: Ordered a -> Bool
inc (Decreasing _) = False
inc _ = True
dec :: Ordered a -> Bool
dec (Increasing _) = False
dec _ = True
increasing :: (Foldable t, Ord a) => t a -> Bool
increasing = inc . foldl' go Empty where
go Empty y = Increasing y
go (Decreasing x) _ = Decreasing x
go (Increasing x) y
| x <= y = Increasing y
| otherwise = Decreasing y
decreasing :: (Foldable t, Ord a) => t a -> Bool
decreasing = dec . foldl' go Empty where
go Empty y = Decreasing y
go (Increasing x) _ = Increasing x
go (Decreasing x) y
| x >= y = Decreasing y
| otherwise = Increasing y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment