Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Looks like you're trying to solve some logic puzzle by unique selection from finite domain. Consult these:</p> <ul> <li><a href="https://stackoverflow.com/questions/9831374/euler-43-is-there-a-monad-to-help-write-this-list-comprehension/9889702#9889702">Euler 43 - is there a monad to help write this list comprehension?</a></li> <li><a href="https://stackoverflow.com/questions/12869097/splitting-list-into-a-list-of-possible-tuples/12872133#12872133">Splitting list into a list of possible tuples</a></li> </ul> <p>The way this helps us is, we carry our domain around while we're making picks from it; and the next pick is made from the narrowed domain containing what's left after the previous pick, so a chain is naturally formed. E.g.</p> <pre><code>p43 = sum [ fromDigits [v0,v1,v2,v3,v4,v5,v6,v7,v8,v9] | (dom5,v5) &lt;- one_of [0,5] [0..9] -- [0..9] is the , (dom6,v6) &lt;- pick_any dom5 -- initial domain , (dom7,v7) &lt;- pick_any dom6 , rem (100*d5+10*d6+d7) 11 == 0 .... -- all possibilities of picking one elt from a domain pick_any :: [a] -&gt; [([a], a)] pick_any [] = [] pick_any (x:xs) = (xs,x) : [ (x:dom,y) | (dom,y) &lt;- pick_any xs] -- all possibilities of picking one of provided elts from a domain -- (assume unique domains, i.e. no repetitions) one_of :: (Eq a) =&gt; [a] -&gt; [a] -&gt; [([a], a)] one_of ns xs = [ (ys,y) | let choices = pick_any xs, n &lt;- ns, (ys,y) &lt;- take 1 $ filter ((==n).snd) choices ] </code></pre> <hr> <p>You can trivially check a number of elements in your answer as a part of your list comprehension:</p> <pre><code>s = [answer | a &lt;- .... , let answer=[....] , length answer==4 ] </code></pre> <p>or just create different answers based on a condition,</p> <pre><code>s = [answer | a &lt;- .... , let answer=if condition then [a,b,c] else [a]] </code></pre>
 

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