An alternative could be to use the right fold:
fun :: (Foldable t, Num a, Eq a) => ta -> a -> [[a]] fun = foldr go $ \a -> if a == 0 then [[]] else [] where go xfa = fa ++ ((x:) <$> f (a - x))
then
\> fun [3,7,5,9,13,17] 30 [[13,17],[3,5,9,13]] \> fun [3,7,5,9,13,17] 12 [[7,5],[3,9]]
The advantage of this approach is that it does not create any lists if it does not add to the desired value. Whereas the filtration-based approach will create all possible lists of subsequences in order to remove most of them during the filtering phase.
behzad.nouri Mar 29 '16 at 17:42 on 2016-03-29 17:42
source share