Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, so what you need is a function <code>combinations n</code> that will produce all the booleans combinations of length <code>n</code>; let's represent them as lists of lists of booleans (i.e. a single assignment of variables will be a list of booleans). Then this function would do the job:</p> <pre><code>let rec combinations = function | 0 -&gt; [[]] | n -&gt; let rest = combinations (n - 1) in let comb_f = List.map (fun l -&gt; false::l) rest in let comb_t = List.map (fun l -&gt; true::l) rest in comb_t @ comb_f </code></pre> <p>There is only one empty combination of length <code>0</code> and for <code>n &gt; 0</code> we produce combinations of <code>n-1</code> and prefix them with <code>false</code> and with <code>true</code> to produce all possible combinations of length <code>n</code>.</p> <p>You could write a function to print such combinations, let's say:</p> <pre><code>let rec combinations_to_string = function | [] -&gt; "" | x::xs -&gt; let rec bools_to_str = function | [] -&gt; "" | b::bs -&gt; Printf.sprintf "%s%s" (if b then "T" else "F") (bools_to_str bs) in Printf.sprintf "[%s]%s" (bools_to_str x) (combinations_to_string xs) </code></pre> <p>and then test it all with:</p> <pre><code>let _ = let n = int_of_string Sys.argv.(1) in let combs = combinations n in Printf.eprintf "combinations(%d) = %s\n" n (combinations_to_string combs) </code></pre> <p>to get:</p> <pre><code>&gt; ./combinations 3 combinations(3) = [TTT][TTF][TFT][TFF][FTT][FTF][FFT][FFF] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload