The Haskell expression allows us to fairly easily define the poweret function:
import Control.Monad (filterM) powerset :: [a] -> [[a]] powerset = filterM (const [True, False])
In order to be able to perform my task, it is important that the mentioned set of permissions be sorted by a specific function, so my kind of implementation looks like this:
import Data.List (sortBy) import Data.Ord (comparing) powersetBy :: Ord b => ([a] -> b) -> [a] -> [[a]] powersetBy f = sortBy (comparing f) . powerset
Now my question is whether there is a way to generate a subset for the parameter set given by the specific start and end point, where f(start) < f(end) and |start| < |end| |start| < |end| . For example, my parameter is a list of integers ( [1,2,3,4,5] ), and they are sorted by their sum. Now I want to extract only subsets in a given range, say 3 to 7 . One way to achieve this would be to: filter set of privileges to include only my range, but this seems (and) ineffective when working with larger subsets:
badFunction :: Ord b => b -> b -> ([a] -> b) -> [a] -> [[a]] badFunction start end f = filter (\x -> fx >= start && fx <= end) . powersetBy f
badFunction 3 7 sum [1,2,3,4,5] produces [[1,2],[3],[1,3],[4],[1,4],[2,3],[5],[1,2,3],[1,5],[2,4],[1,2,4],[2,5],[3,4]] .
My question now is whether there is a way to generate this list directly, without having to generate all 2^n subsets in the first place, since it will significantly improve performance by not checking all the elements, but rather by generating them on the fly. "