Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>{- It depends on what is "smallest". But here is a solution for a concept of "smallest" if tuples were compared first by their max. number and then by their total sum. (You can just copy and paste my whole answer into a file as I write the text in comments.)</p> <p>We will need <code>nub</code> later. -}</p> <pre><code>import Data.List (nub) </code></pre> <p>{- Just for illustration: the easy case with 2-tuples. -}</p> <pre><code>-- all the two-tuples where 'snd' is 'n' tuples n = [(i, n) | i &lt;- [1..n]] -- all the two-tuples where 'snd' is in '1..n' tuplesUpTo n = concat [tuples i | i &lt;- [1..n]] </code></pre> <p>{- To get all results, you will need to insert the flip of each tuple into the stream. But let's do that later and generalize first. </p> <p>Building tuples of arbitrary length is somewhat difficult, so we will work on lists. I call them 'kList's, if they have a length 'k'. -}</p> <pre><code>-- just copied from the tuples case, only we need a base case for k=1 and -- we can combine all results utilizing the list monad. kLists 1 n = [[n]] kLists k n = do rest &lt;- kLists (k-1) n add &lt;- [1..head rest] return (add:rest) -- same as above. all the klists with length k and max number of n kListsUpTo k n = concat [kLists k i | i &lt;- [1..n]] -- we can do that unbounded as well, creating an infinite list. kListsInf k = concat [kLists k i | i &lt;- [1..]] </code></pre> <p>{- The next step is rotating these lists around, because until now the largest number is always in the last place. So we just look at all rotations to get all the results. Using <code>nub</code> here is admittedly awkward, you can improve that. But without it, lists where all elements are the same are repeated <code>k</code> times. -}</p> <pre><code>rotate n l = let (init, end) = splitAt n l in end ++ init rotations k l = nub [rotate i l | i &lt;- [0..k-1]] rotatedKListsInf k = concatMap (rotations k) $ kListsInf k </code></pre> <p>{- What remains is to convert these lists into tuples. This is a bit awkward, because every n-tuple is a separate type. But it's straightforward, of course. -}</p> <pre><code>kListToTuple2 [x,y] = (x,y) kListToTuple3 [x,y,z] = (x,y,z) kListToTuple4 [x,y,z,t] = (x,y,z,t) kListToTuple5 [x,y,z,t,u] = (x,y,z,t,u) kListToTuple6 [x,y,z,t,u,v] = (x,y,z,t,u,v) </code></pre> <p>{- Some tests:</p> <pre><code>*Main&gt; take 30 . map kListToTuple2 $ rotatedKListsInf 2 [(1,1),(1,2),(2,1),(2,2),(1,3),(3,1),(2,3),(3,2),(3,3),(1,4),(4,1),(2,4),(4,2),(3,4), (4,3),(4,4),(1,5),(5,1),(2,5),(5,2),(3,5),(5,3),(4,5),(5,4),(5,5),(1,6),(6,1), (2,6), (6,2), (3,6)] *Main&gt; take 30 . map kListToTuple3 $ rotatedKListsInf 3 [(1,1,1),(1,1,2),(1,2,1),(2,1,1),(1,2,2),(2,2,1),(2,1,2),(2,2,2),(1,1,3),(1,3,1), (3,1,1),(1,2,3),(2,3,1),(3,1,2),(2,2,3),(2,3,2),(3,2,2),(1,3,3),(3,3,1),(3,1,3), (2,3,3),(3,3,2),(3,2,3),(3,3,3),(1,1,4),(1,4,1),(4,1,1),(1,2,4),(2,4,1),(4,1,2)] </code></pre> <p><em><strong>Edit:</em></strong> I realized there is a bug: Just rotating the ordered lists isn't enough of course. The solution must be somewhere along the lines of having </p> <pre><code>rest &lt;- concat . map (rotations (k-1)) $ kLists (k-1) n </code></pre> <p>in <code>kLists</code>, but then some issues with repeated outputs arise. You can figure that out, I guess. ;-) -}</p>
    singulars
    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