Hello again Ryan,<br><br>I have found out where to import those stuff from and tested your more elegant suggestion and my original performance.<br><br><span style="font-family: courier new,monospace;">-- &nbsp;&nbsp;&nbsp; print ((length ∘ pmsO [0,1]) 24) 9~&nbsp; seconds</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">--&nbsp;&nbsp;&nbsp;&nbsp; print ((length ∘ pmsE [0,1]) 24) 23~ seconds</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">-- &nbsp;&nbsp;&nbsp; print ((length ∘ pmsU [0,1]) 24) 23~ seconds</span><br style="font-family: courier new,monospace;">
<br>-- O: original, E: elegant, U: unreadable<br><br>I prefer performance over elegance (and for me using monads almost feels like adding an unnecessary dependency to the definition of pms. Though I know I may be dead wrong on that. I just don&#39;t quite understand monads yet.)<br>
<br>I would love to have you and/or others suggest more performant versions of pms (and maybe also come up with a better name for it).<br><br>mnt :: [a] → [[a]] → [[a]]<br>mnt []&nbsp;&nbsp;&nbsp;&nbsp; _&nbsp;&nbsp; = []<br>mnt _&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; []&nbsp; = []<br>mnt (x:xs) yss = map (x:) yss ++ mnt xs yss<br>
<br>pms :: [a] → Int → [[a]]<br>pms [] _&nbsp; = [[]]<br>pms _&nbsp; 0&nbsp; = [[]]<br>pms xxs n = mnt xxs (pms xxs (n - 1))<br><br>I generalized &#39;pms&#39; from the &#39;bools&#39; function on page 108 of Programming in Haskell (Hutton, 2007)<br>
<br>-- Cetin Sert<br><br><div><span class="gmail_quote">On 26/01/2008, <b class="gmail_sendername">Ryan Ingram</b> &lt;<a href="mailto:ryani.spam@gmail.com">ryani.spam@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
When you say permuations, I think of reorderings of a list, for example:<br><br>permutations [1,2,3] =<br>[ [1,2,3],<br>&nbsp;&nbsp;[1,3,2],<br>&nbsp;&nbsp;[2,1,3],<br>&nbsp;&nbsp;[2,3,1],<br>&nbsp;&nbsp;[3,1,2],<br>&nbsp;&nbsp;[3,2,1] ]<br><br>Here&#39;s an implementation:<br>
<br>-- split [1,2,3] =&gt; [<br>--&nbsp;&nbsp;&nbsp;&nbsp;( 1, [2,3] ),<br>--&nbsp;&nbsp;&nbsp;&nbsp;( 2, [1,3] ),<br>--&nbsp;&nbsp;&nbsp;&nbsp;( 3, [1,2] ) ]<br>split :: [a] -&gt; [(a, [a])]<br>split [] = error &quot;split: empty list&quot;<br>split [a] = [(a, [])]<br>split (a:as) = (a, as) : map prefix (split as)<br>
&nbsp;&nbsp;&nbsp;&nbsp;where prefix (x, xs) = (x, a : xs)<br><br>permutations :: [a] -&gt; [[a]]<br>permutations [] = return []<br>permutations xs = do<br>&nbsp;&nbsp;&nbsp;&nbsp;(first, rest) &lt;- split xs<br>&nbsp;&nbsp;&nbsp;&nbsp;rest&#39; &lt;- permutations rest<br>&nbsp;&nbsp;&nbsp;&nbsp;return (first : rest&#39;)<br>
<br>The problem you solved can be solved much more elegantly:<br><br>pms : [a] -&gt; Int -&gt; [[a]]<br>pms xs n = foldM combine [] (replicate n xs) where<br>&nbsp;&nbsp; combine rest as = liftM (:rest) as<br><br>or, for the unreadable version:<br>
pms xs n = foldM (map . flip (:)) [] $ replicate n xs<br><br>(note that, in the list monad, liftM = map).<br><br>&nbsp;&nbsp;-- ryan<br></blockquote></div><br>