プログラミングのお題スレ Part3
■ このスレッドは過去ログ倉庫に格納されています
0035デフォルトの名無しさん
2014/01/27(月) 20:10:40.70groupBySorted :: Ord a => [a] -> [[a]]
groupBySorted [] = []
groupBySorted (x0:xs0) = ls0 : groupBySorted rs0
where
(ls0, rs0) = f x0 xs0
f x [] = ([x], [])
f x (y:ys)
| x <= y = let (ls, rs) = f y ys in (x:ls, rs)
| otherwise = ([x], y:ys)
main :: IO ()
main = do
test [8,3,4,9,9,10,6,1,4,3] -- => [[8],[3,4,9,9,10],[6],[1,4],[3]]
test [1,3,3,4,4,6,8,9,9,10] -- => [[1,3,3,4,4,6,8,9,9,10]]
test [10,9,9,8,6,4,4,3,3,1] -- => [[10],[9,9],[8],[6],[4,4],[3,3],[1]]
test [1,1,1,1,1,1,1,1,1,1] -- => [[1,1,1,1,1,1,1,1,1,1]]
test ([] :: [Int]) -- => []
test [1] -- => [[1]]
test :: (Ord a, Show a) => [a] -> IO ()
test xs = putStrLn $ show xs ++ " => " ++ show (groupBySorted xs)
■ このスレッドは過去ログ倉庫に格納されています