Here we need to get all subsequences of the given length. How to calculate the asymptotic complexity of the given function?
import Data.List
subsequencesOfSize l n = [x | x <- subsequences l, length x == n]
How many "iterations" will it be? Can we speak about "iterations". Any optimizations? GHC 7.10.2. How compiler or programmer can improve this?
Here is a related SO question which discusses various implementations of subsequencesOfSize
:
Haskell: comparison of techniques for generating combinations
and the best performing one which uses memoization:
Haskell: comparison of techniques for generating combinations
Update
Also consider this version that @user3237465 mentions in the comments:
subsequencesOfSize :: Int -> [a] -> [[a]]
subsequencesOfSize n xs | n > length xs = []
subsequencesOfSize n xs = subsequencesBySize xs !! n where
subsequencesBySize [] = [[[]]]
subsequencesBySize (x:xs) = zipWith (++) ([] : map (map (x:)) next) (next ++ [[]])
where next = subsequencesBySize xs