Skip to content

Instantly share code, notes, and snippets.

@gofer
Created November 21, 2017 14:54
Show Gist options
  • Save gofer/a1d5b03eeca32d7e5f672fdf85aafdeb to your computer and use it in GitHub Desktop.
Save gofer/a1d5b03eeca32d7e5f672fdf85aafdeb to your computer and use it in GitHub Desktop.
Sub List
(* subList : 'a list -> 'a list list *)
fun subList v =
List.foldl
List.@
[ nil ]
(
List.map
(
fn x => (
fn v =>
List.map
(fn x => List.take (v, x))
(List.tabulate ((List.length v), (fn x => x + 1)))
)
(List.drop (v, x))
)
(List.rev(List.tabulate ((List.length v) + 1, (fn x => x))))
);
(* Example*)
val v = [1, 2, 3, 4, 5, 6];
val () = print ((listToString (fn v => listToString Int.toString v) (subList v)) ^ "\n");
val v = ["A", "B", "C", "D", "E"];
val () = print ((listToString (fn v => listToString (fn s => s) v) (subList v)) ^ "\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment