From 5a1bfa3b3192df57cc06a8e7312454c4f0ffafc5 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot <378351+weskerfoot@users.noreply.github.com> Date: Mon, 27 Jan 2020 14:18:50 -0500 Subject: [PATCH] Create sorted_digits.hs --- sorted_digits.hs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 sorted_digits.hs diff --git a/sorted_digits.hs b/sorted_digits.hs new file mode 100644 index 0000000..7b6b3df --- /dev/null +++ b/sorted_digits.hs @@ -0,0 +1,18 @@ +import Data.List + +digits :: Int -> [Int] +digits n = unfoldr nextDigit n where + nextDigit 0 = Nothing + nextDigit c = Just $ (c `mod` 10, c `div` 10) + +isSorted :: Int -> Bool +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