online advertising

Monday, June 8, 2015

Haskell 99 Problem 27

Problem

Please find the problem here.

Solution:

Please see the solution as follow:

While the code is concise - it needs to be explained (even to myself). First, a helper function combineWithRest is defined so that instead of just computing the combinations, the function also capture the rest of the list that is not in the combination.

To ease the description, Let's call [a] a combination (an example would be [1,2]), [[a]] a group (an example would be [[1,2],[3, 4]]. The basic algorithm is to first combination size requirements. For each combination, we then use the rest of the list to form groups with the remaining combination size requirements. To get the correct groups, we append the first combination to the groups. Therefore, we get a list of group per combination, and therefore we concatenate them to get what we need.

No comments :

Post a Comment