(* finding annotated, nonempty, maximal, strictly increasing sublists *) (* we say that (n, m, xs) is an annotated, nonempty, maximal, strictly increasing sublist (ANMSIS) of a list ys iff n, m are natural numbers, and n <= m, and xs is elements n through m of ys (counting from 0), and the elements of xs appear in strictly increasing order, and element n - 1 of ys (if it exists) isn't < the first element of xs, and element m + 1 of ys (it it exists) isn't > the last element of xs *) (* val maxi : (int * int * 'a list)list -> (int * 'a * 'a list)option -> int -> 'a list -> (int * int * 'a list)list if cur = None, then maxi anss cur n ys returns List.rev anss @ ([(i1 + n, j1 + n, x1s), (i2 + n, j2 + n, x2s), ...] where (i1, j1, x1s), (i2, j2, x2s), ..., are the ANMSISs of ys if cur = Some(m, x, xs) and ys has a head > x, then maxi anss cur n ys returns List.rev anss @ [(m, j1 + n, List.rev(x :: xs) @ x1s), (i2 + n, j2 + n, x2s), ...] where (i1, j1, x1s), (i2, j2, x2s), ..., are the ANMSISs of ys if cur = Some(m, x, xs) and ys is [] or has a head not > x, then maxi anss cur n ys returns List.rev anss @ [(m, n - 1, List.rev(x :: xs)), (i1 + n, j1 + n, x1s), (i2 + n, j2 + n, x2s), ...] where (i1, j1, x1s), (i2, j2, x2s), ..., are the ANMSISs of ys --------------------------------------------------------------------------- TERMINATION the size of a value of type (int * 'a * 'a list)option is defined by: size None = 0 size (m, x, xs) = 1 + length xs when maxi anss cur n ys makes a recursive call maxi ans's cur' n' y's, either size cur' + length y's = size cur + length ys and length y's < length ys, or size cur' + length y's < size cur + length ys *) let rec maxi anss cur n ys = match cur with None -> (match ys with [] -> List.rev anss | y :: ys -> maxi anss (Some(n, y, [])) (n + 1) ys) | Some(m, x, xs) -> match ys with [] -> List.rev((m, n - 1, List.rev(x :: xs)) :: anss) | y :: ys as y_ys -> if y > x then maxi anss (Some(m, y, x :: xs)) (n + 1) ys else maxi ((m, n - 1, List.rev(x :: xs)) :: anss) None n y_ys (* val maximal : 'a list -> (int * int * 'a list)list maximal ys returns the list of all ANMSISs of ys, ordered according to their first components *) let maximal ys = maxi [] None 0 ys (* try: maximal[1;3;5;5;2;8;2];; *) (* try: let test k = let rec tst i j k xs = if j > k then List.rev xs else if i > j then tst 0 (j + 1) k xs else tst (i + 1) j k (i :: xs) in if k < 1 then [] else tst 0 0 k [];; let xs = test 2000;; List.length xs;; maximal xs;; *)