我尝试运行中等输入时内存不足,例如:
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?
我有下面的函数定义用于map-edit
(def map-edit
(fn [m lst k f]
(if (car lst)
(assoc m
(car lst)
(map-edit (get m (car lst) {}) k f))
(assoc m k (f (get m k))))))
Run Code Online (Sandbox Code Playgroud)
当我尝试在我的repl中调用此函数时
(map-edit {} (list "oeu") "oeuoeu" (fn [q] "oeu"))
Run Code Online (Sandbox Code Playgroud)
我得到了Arity的错误
ArityException Wrong number of args (3) passed to: core/map-edit clojure.lang.AFn.throwArity (AFn.java:429)
Run Code Online (Sandbox Code Playgroud)
为什么它认为我只传递3个参数?
; CIDER 0.8.2 (Java 1.8.0_121, Clojure 1.8.0, nREPL 0.2.12)
Run Code Online (Sandbox Code Playgroud)