How do I generate memoized recursive functions in Clojure?

前端 未结 8 1038
[愿得一人]
[愿得一人] 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:27

    Here is the simplest solution:

    (def fibo
      (memoize (fn [n]
                 (if (< n 2)
                   n
                   (+ (fibo (dec n))
                      (fibo (dec (dec n))))))))
    

提交回复
热议问题