gpt4 book ai didi

let 函数内的递归

转载 作者:行者123 更新时间:2023-12-03 07:01:03 31 4
gpt4 key购买 nike

我对 def 和 let 如何以不同的方式绑定(bind)变量感到困惑。有人可以向我解释为什么这有效吗:

(def leven
(memoize
(fn [x y]
(cond (empty? x) (count y)
(empty? y) (count x)
:else (min (+ (leven (rest x) y) 1)
(+ (leven x (rest y)) 1)
(+ (leven (rest x) (rest y)) (if (= (first x) (first y)) 0 1)))))))

但是当我尝试将函数声明为 let 时,它无法编译:

(def leven
(let [l (memoize (fn [x y]
(cond (empty? x) (count y)
(empty? y) (count x)
:else (min (+ (l (rest x) y) 1)
(+ (l x (rest y)) 1)
(+ (l (rest x) (rest y)) (if (= (first x) (first y)) 0 1))))))]
(l x y)))

编辑:这有效,使用 Ankur 展示的技术。

(defn leven [x y]
(let [l (memoize (fn [f x y]
(cond (empty? x) (count y)
(empty? y) (count x)
:else (min (+ (f f (rest x) y) 1)
(+ (f f x (rest y)) 1)
(+ (f f (rest x) (rest y)) (if (= (first x) (first y)) 0 1))))))
magic (partial l l)]
(magic x y)))

最佳答案

下面是一个执行您要求的示例。我使用阶乘只是为了简单起见,并在阶乘中添加了 println 以确保内存工作正常

(let [fact (memoize (fn [f x] 
(println (str "Called for " x))
(if (<= x 1) 1 (* x (f f (- x 1))))))
magic (partial fact fact)]
(magic 10)
(magic 11))

首先计算 10 的阶乘,然后计算 11,在这种情况下,不应再次调用 10 的阶乘直到 1,因为这已被记住。

Called for 10
Called for 9
Called for 8
Called for 7
Called for 6
Called for 5
Called for 4
Called for 3
Called for 2
Called for 1
Called for 11
39916800

关于let 函数内的递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12955024/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com