我尝试运行中等输入时内存不足,例如:
variation_models 15 25
同样为 ncars 运行更高的数字似乎在速度和内存使用方面产生了巨大的差异。
放缓是预料之中的(有更多的东西可以比较),但内存使用量的指数增长对我来说没有意义
import Control.Monad
orderedq f [] = True
orderedq f (x:[]) = True
orderedq f (x:y:zs) = f x y && orderedq f (y:zs)
num_orderedq = orderedq (<=)
adds_up_to n xs = n == sum xs
both_conditions f g xs = f xs && g xs
variation_models ncars nlocations =
filter (both_conditions (adds_up_to nlocations) num_orderedq) $ replicateM ncars [1..nlocations-ncars+1]
Run Code Online (Sandbox Code Playgroud)
是什么导致了内存使用量的巨大差异?replicateM?