プログラミングのお題スレ Part3
■ このスレッドは過去ログ倉庫に格納されています
0034デフォルトの名無しさん
2014/01/27(月) 20:05:31.93import Control.Monad
isSorted :: Ord a => [a] -> Either Int Ordering
isSorted [] = Right EQ
isSorted [_] = Right EQ
isSorted xs = foldM f EQ $ zip [1..] $ zipWith compare xs $ tail xs
where
f EQ (_,x) = Right x
f acc (_,EQ) = Right acc
f acc (i,x)
| acc == x = Right acc
| otherwise = Left i
main :: IO ()
main = do
test [6,6,3,2,6,4,7,4,7,4] -- => 4
test [2,3,4,4,4,6,6,6,7,7] -- => AS
test [7,7,6,6,6,4,4,4,3,2] -- => DES
test [1,1,1,1,1,1,1,1,1,1] -- => EQ
test ([] :: [Int]) -- => EQ
test [1] -- => EQ
test :: (Ord a, Show a) => [a] -> IO ()
test xs = do
putStr $ show xs ++ " => "
putStrLn $ case isSorted xs of
Left i -> show i
Right EQ -> "EQ"
Right LT -> "AS"
Right GT -> "DES"
■ このスレッドは過去ログ倉庫に格納されています