我实现了这个答案的一个版本https://stackoverflow.com/a/9920425/1261166(我不知道回答者的意图)
(I don't know what was intended by the person answering) 这句话的意思是“我不知道回答者的意图”。
sublistofsize 0 _ = [[]]
sublistofsize _ [] = []
sublistofsize n (x : xs) = sublistsThatStartWithX ++ sublistsThatDontStartWithX
where sublistsThatStartWithX = map (x:) $ sublistofsize (n-1) xs
sublistsThatDontStartWithX = sublistofsize n xs
我不确定的是 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 0.0
sublistofsize.sublistsThatStartWithX Main 116 2369935 47.8 60.1 47.8 60.1
我实现的方式是否好?有没有更快的方法?
map
不会改变这一点。 - GS - Apologise to Monica