Skip to main content
Notice removed Insufficient justification by Jamal
Added explanations
Source Link

Your code is totally imperative. In some cases (probably in most) it's faster, but this not the best way to use OCaml :) Here is my solution in functional style:

Printing the list of lists can be done by iterating over list of lists:

let print lst =
  List.iter (fun l ->
      List.map string_of_int l
      |> String.concat " "
      |> print_endline
    ) lst

let rec permutations result other = function
  | [] -> [result]
  | hd :: tl ->
    let r = permutations (hd :: result) [] (other @ tl) in
    if tl <> [] then
      r @ permutations result (hd :: other) tl
    else
      r

let () =
  permutations [] [] [1; 2; 3]
  |> print
let print lst =
  List.iter (fun l ->
      List.map string_of_int l
      |> String.concat " "
      |> print_endline
    ) lst

Next recursive function does:

  • Selects head element of the list and makes it heading element of the resulting list
  • Recursively calls itself on the list of all previous elements (minus resulting subset) + tail.
let rec permutations result other = function
  | [] -> [result]
  | hd :: tl ->
    let r = permutations (hd :: result) [] (other @ tl) in
    if tl <> [] then
      r @ permutations result (hd :: other) tl
    else
      r

All together. Initial result is empty and the stored list of previous elements is also empty:

let () =
  permutations [] [] [1; 2; 3]
  |> print

Your code is totally imperative. In some cases (probably in most) it's faster, but this not the best way to use OCaml :) Here is my solution in functional style:

let print lst =
  List.iter (fun l ->
      List.map string_of_int l
      |> String.concat " "
      |> print_endline
    ) lst

let rec permutations result other = function
  | [] -> [result]
  | hd :: tl ->
    let r = permutations (hd :: result) [] (other @ tl) in
    if tl <> [] then
      r @ permutations result (hd :: other) tl
    else
      r

let () =
  permutations [] [] [1; 2; 3]
  |> print

Your code is totally imperative. In some cases (probably in most) it's faster, but this not the best way to use OCaml :) Here is my solution in functional style:

Printing the list of lists can be done by iterating over list of lists:

let print lst =
  List.iter (fun l ->
      List.map string_of_int l
      |> String.concat " "
      |> print_endline
    ) lst

Next recursive function does:

  • Selects head element of the list and makes it heading element of the resulting list
  • Recursively calls itself on the list of all previous elements (minus resulting subset) + tail.
let rec permutations result other = function
  | [] -> [result]
  | hd :: tl ->
    let r = permutations (hd :: result) [] (other @ tl) in
    if tl <> [] then
      r @ permutations result (hd :: other) tl
    else
      r

All together. Initial result is empty and the stored list of previous elements is also empty:

let () =
  permutations [] [] [1; 2; 3]
  |> print
Notice added Insufficient justification by Jamal
Source Link

Your code is totally imperative. In some cases (probably in most) it's faster, but this not the best way to use OCaml :) Here is my solution in functional style:

let print lst =
  List.iter (fun l ->
      List.map string_of_int l
      |> String.concat " "
      |> print_endline
    ) lst

let rec permutations result other = function
  | [] -> [result]
  | hd :: tl ->
    let r = permutations (hd :: result) [] (other @ tl) in
    if tl <> [] then
      r @ permutations result (hd :: other) tl
    else
      r

let () =
  permutations [] [] [1; 2; 3]
  |> print