How do I generate memoized recursive functions in Clojure?

前端 未结 8 1020
[愿得一人]
[愿得一人] 2020-12-25 11:31

I\'m trying to write a function that returns a memoized recursive function in Clojure, but I\'m having trouble making the recursive function see its own memoized bindings. I

8条回答
  •  轮回少年
    2020-12-25 12:12

    Your first version actually works, but you're not getting all the benefits of memoization because you're only running through the algorithm once.

    Try this:

    user>  (time (let [f (make-fibo 1)]
              (f 35)))
    "Elapsed time: 1317.64842 msecs"
    14930352
    
    user>  (time (let [f (make-fibo 1)]
              [(f 35) (f 35)]))
    "Elapsed time: 1345.585041 msecs"
    [14930352 14930352]
    

提交回复
热议问题