Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Disclaimer: I don't have a machine with the current F# installed, so I can't test my code. Basically, though, if you steal <code>sequence</code> from Haskell, you can write your program as</p> <pre><code>let cartesian = sequence &gt;&gt; List.map product </code></pre> <p>and run it as</p> <pre><code>cartesian [[1..99]; [1..99]] </code></pre> <p>Here's how to write <code>sequence</code>. It's a generalised version of the sequence expression you wrote. It just handles an unlimited number of lists: <code>{ for x in xs do for y in ys do for z in zs ... yield [x;y;z;...] }</code>.</p> <pre><code>let rec sequence = function | [] -&gt; Seq.singleton [] | (l::ls) -&gt; seq { for x in l do for xs in sequence ls do yield (x::xs) } // also you'll need product to do the multiplication let product = Seq.fold_left1 ( * ) </code></pre> <p>Then you can write your program as</p> <pre><code>let cartesian xs ys = [xs; ys] |&gt; sequence |&gt; List.map product // ... or one-argument, point-free style: let cartesian' = sequence &gt;&gt; Seq.map product </code></pre> <p>You might have to change some <code>Seq</code>s to <code>List</code>s.</p> <p>However, the number of people who can guess the meaning of your non-general list comprehension is probably a lot more than will recognise the name <code>sequence</code>, so you're probably better off with the list comprehension. <code>sequence</code> comes in handy any time you want to run a whole list of computation expressions, though.</p>
 

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