Writing a function to get all subsequences of size n in Haskell?

I tried to write a function to get all subsequences of a list of size n, but I'm not sure how to do this.

I thought that I could probably use the built-in Data.List.subsequences and just filter out lists that don't have size n, but this seems like a pretty roundabout and inefficient way to do this, d rather don't do this if I can avoid of this, so I wonder if you have any ideas?

I want it to be something like this type

subseqofsize :: Int -> [a] -> [[a]]

For further explanation, here is an example of what I'm looking for:

subseqofsize 2 [1,2,3,3]
[[1,2],[1,3],[2,3],[1,3],[2,3],[3,3]]

In addition, I do not care about the order of anything.

+2
source share
2

, , , , , , .

, .

:

sublistofsize 0 _        = ...
sublistofsize _ []       = ...

:

sublistofsize n (x : xs) = sublistsThatStartWithX ++ sublistsThatDon'tStartWithX
  where sublistsThatStartWithX = ...
        sublistsThatDon'tStartWithX = ...

, . : , .

?

+12

: k x ; , . x, k-1 , . k , , x. () .

( , .)

(: , dave4420:))

+2

Source: https://habr.com/ru/post/1526907/


All Articles