A collection of implementations of common algorithms
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
447 B

import Data.List
digits :: Int -> [Int]
digits n = unfoldr nextDigit n where
nextDigit 0 = Nothing
4 years ago
nextDigit c = Just (c `mod` 10, c `div` 10)
isSorted :: Int -> Bool
4 years ago
isSorted n = all (uncurry (<=)) ds' || all (uncurry (>=)) ds''
where ds = digits n
ds' = zip ds (maxBound : ds)
ds'' = zip ds (minBound : ds)
main = do
print $ isSorted 123456
print $ isSorted 1234561
print $ isSorted 97541
print $ isSorted 99431