g :: [Int] -> [Int] g [] = [] g xs = let X m ys = replace_with_min m xs in ys where -- replace elements with min and return the tuple of min and replaced list replace_with_min :: Int -> [Int] -> X replace_with_min m [x] = X x [m] replace_with_min m (x : xs) = let X m' xs' = replace_with_min m xs in X (min x m') (m : xs')