相关疑难解决方法(0)

从n返回k个元素的所有组合的算法

我想写一个函数,它将一个字母数组作为参数,并选择一些字母.

假设您提供了8个字母的数组,并希望从中选择3个字母.然后你应该得到:

8! / ((8 - 3)! * 3!) = 56
Run Code Online (Sandbox Code Playgroud)

数组(或单词)返回,每个包含3个字母.

algorithm combinations

551
推荐指数
23
解决办法
43万
查看次数

列表性能的长度为n的子序列

我实现了这个答案的一个版本/sf/answers/694429781/(我不知道回答的人的意图是什么)

sublistofsize 0 _        = [[]]
sublistofsize _ []       = []
sublistofsize n (x : xs) = sublistsThatStartWithX ++ sublistsThatDontStartWithX
  where sublistsThatStartWithX = map (x:) $ sublistofsize (n-1) xs
        sublistsThatDontStartWithX = sublistofsize n xs
Run Code Online (Sandbox Code Playgroud)

我不确定的是什么 sublistsThatStartWithX = map (x:) $ sublistofsize (n-1) xs

我假设map(x :)在性能方面提出了问题,但不确定如何解决它.我做过剖析print $ length $ sublistofsize 5 $ primesToTakeFrom 50

COST CENTRE                                  MODULE                                        no.     entries  %time %alloc   %time %alloc
sublistofsize                             Main                                          112     4739871   46.9   39.9    96.9  100.0
 sublistofsize.sublistsThatDontStartWithX Main                                          124     2369935    2.2    0.0     2.2 …
Run Code Online (Sandbox Code Playgroud)

performance haskell

4
推荐指数
1
解决办法
3785
查看次数

标签 统计

algorithm ×1

combinations ×1

haskell ×1

performance ×1