Reimplement Trampoline
Difficulty: | Medium |
Topics: | core-functions |
Reimplement the function described in "Intro to Trampoline".
(= (letfn [(triple [x] #(sub-two (* 3 x)))
(sub-two [x] #(stop?(- x 2)))
(stop? [x] (if (> x 50) x #(triple x)))] (__ triple 2)) 82)
(= (letfn [(my-even? [x] (if (zero? x) true #(my-odd? (dec x))))
(my-odd? [x] (if (zero? x) false #(my-even? (dec x))))]
(map (partial __ my-even?) (range 6))) [true false true false true false])
解长度 41:
#(loop [t (apply % %&)] (if (fn? t) (recur (t)) t))
时间: 2024-10-05 05:01:59