Here is my functional approach to split a list into sub lists, each having non-decreasing numbers, so the input
\$[1; 2; 3; 2; 4; 1; 5;]\$
\$[[1; 2; 3]; [2; 4]; [1; 5]]\$
The code works, but I need a while to understand it. I would prefer a solution that is more clear. Is there a way to make this more elegant or readable? Everything is allowed, F# mutables as well. Or do I just get used more to functional code reading?
The subsets are called "run"s in the code below.
// returns a single run and the remainder of the input let rec takerun input = match input with |  -> ,  | [x] -> [x],  | x :: xr -> if x < xr.Head then let run, remainder = takerun xr x :: run, remainder else [x], xr // returns the list of all runs let rec takeruns input = match takerun input with | run,  -> [run] | run, rem -> run :: takeruns rem let runs = takeruns [1; 2; 3; 2; 4; 1; 5;] > val runs : int list list = [[1; 2; 3]; [2; 4]; [1; 5]]
Considering the helpful feedback I ended up with this reusable code. And got more used to functional programming, comparing imperative alternatives I meanwhile find the pure functional approach more readable. This version is good readable, although not tail recursive. For the small lists I had to deal with, readability was preferred.
// enhance List module module List = // splits list xs into 2 lists, based on f(xn, xn+1) let rec Split f xs = match xs with |  -> ,  | [x] -> [x],  | x1 :: x2 :: xr when f x1 x2 -> [x1], x2 :: xr // split on first f(xn, xn+1) | x :: xr -> let xs1, xs2 = Split f xr x :: xs1, xs2 // Now takruns becomes quite simple let rec takeruns input = match List.Split (>) input with | run,  -> [run] | run, rem -> run :: takeruns rem let runs = takeruns [1; 2; 3; 2; 4; 1; 5;]