Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Haskell will perform what is known as lazy evaluation, meaning it will only work things out as it needs them from left to right (generally). So taking your example of <code>mystery [1, 2]</code>, Haskell will do the following:</p> <pre><code>sets ++ (map (x:) sets) </code></pre> <p>Which evaluates to:</p> <pre><code>mystery (2:[]) ++ (map (1:) sets) </code></pre> <p>At this point, we're calling <code>mystery (2:[])</code></p> <pre><code>mystery ([]) ++ (map (2:) sets) ++ (map (1:) sets) </code></pre> <p><code>mystery ([])</code> will return an empty list of lists</p> <pre><code>[[]] ++ (map (2:) sets) ++ (map (1:) sets) [[]] ++ (map (2:) mystery []) ++ (map (1:) sets) </code></pre> <p>So now Haskell will try to apply the function <code>(2:)</code> on a list containing an empty list</p> <pre><code>[[]] ++ (2:[[]]) ++ (map (1:) sets) [[]] ++ [[2]] ++ (map (1:) sets) [[], [2]] ++ (map (1:) sets) </code></pre> <p>This is where things get a little more confusing.</p> <pre><code>[[], [2]] ++ (map (1:) mystery (2:[])) </code></pre> <p>That last <code>sets</code> will evaluate <code>mystery (2:[])</code></p> <pre><code>[[], [2]] ++ (map (1:) (sets ++ (map (2:) sets))) [[], [2]] ++ (map (1:) (mystery [] ++ (map (2:) sets)) [[], [2]] ++ (map (1:) ([[]] ++ (map (2:) mystery [])) [[], [2]] ++ (map (1:) ([[]] ++ (2:[[]])) [[], [2]] ++ (map (1:) ([[]] ++ [[2]]) </code></pre> <p>Now <code>(1:)</code> will be applied to a list which contains an empty list, and a list containing the 2:</p> <pre><code>[[], [2]] ++ (map (1:) ++ [[], [2]]) [[], [2]] ++ [[1], [1, 2]] [[], [2], [1], [1, 2]] </code></pre> <p>The real meat of the operation is in those last two sections. Haskell creates a list like <code>[[], [2]]</code> and then appends one to the head of each list to form <code>[[1], [1, 2]]</code>.</p>
    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. 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