On 15/12/2008, Hakim Cassimally <hakim.cassima...@gmail.com> wrote: > On 15/12/2008, Avleen Vig <avl...@gmail.com> wrote: > > On Dec 15, 2008, at 10:04, "James Laver" <james.la...@gmail.com> wrote: > > > On Sun, Dec 14, 2008 at 3:15 PM, Avleen Vig <avl...@gmail.com> wrote: > > > > On Fri, Dec 12, 2008 at 9:50 AM, Chris Jack <chris_j...@msn.com> wrote: > > > > > 3) Write a Perl function that takes two references to arrays and > > returns the intersect of them. If an entry appears n times in array 1 and m > > times in array 2, the output should list that entry min(n,m) times. Bonus > > mark for one line solutions. > > > > > > > > > In the spirit of sharing, I offer this solution, from your neighbours > > > > in the Python community: > > <snip> > > In the same spirit (and once pndc had corrected me on the spec... > d'oh!) I attempted in Haskell... but pretty much failed at a compact > solution:
Aha! Haskell has an 'intersect' function... though it doesn't quite do the min(n,m) thing, as it shows *all* the repetitions in the LHS. Still, if you look at its source, you can get the following one-liner (which is possibly less efficient than my original?) minIntersect xs ys = let g=group.sort in [ zipWith const x y | x <- g xs, y <- g ys, head x == head y ] osfameron --