首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SICP、接续传球风格与Clojure蹦床

SICP、接续传球风格与Clojure蹦床
EN

Stack Overflow用户
提问于 2014-10-19 13:26:45
回答 1查看 1.1K关注 0票数 1

我和SICP一起工作,锻炼2.29-b,让我有机会在移动电话和支路上继续传球。

为了使故事更短,每个手机都有左右两个分支,它们由一个长度和一个数字权重或另一个移动部分组成。这个问题要求找到给一部手机的总重量。

在完成了第一个相互递归的解决方案(非常简单)之后,我尝试并成功地实现了一个cps的解决方案:

代码语言:javascript
复制
(defn total-weight-cps [mobile]
  (letfn 
    [(branch-weight-cps
      [branch kont]
      (let [structure (branch-structure branch)]
        (if (mobile? (branch-structure branch))
          (do (println "then " structure) (kont (traverse-mobile-cps structure identity)))
          (do (println "else " structure) (kont structure)))))

     (traverse-mobile-cps
      [mobile kont]
      (branch-weight-cps (left-branch mobile)
                         (fn [left-weight]
                           (branch-weight-cps (right-branch mobile)
                                              (fn [right-weight] (kont (+ left-weight right-weight)))))))]

    (traverse-mobile-cps mobile identity)))

在这一点上,我已经尝试应用蹦床,以保存我的堆栈。但是,除以下例外情况外,这是个例外:

代码语言:javascript
复制
java.lang.ClassCastException: sicp_clojure.2_1_exercises_2_24_2_32$total_weight_STAR_$traverse_mobile_cps__6694$fn__6695$fn__6696$fn__6697 cannot be cast to java.lang.Number
Numbers.java:126 clojure.lang.Numbers.add
.../git/sicp-clojure/src/sicp_clojure/2_1_exercises_2_24_2_32.clj:185 sicp-clojure.2-1-exercises-2-24-2-32/total-weight*[fn]
core.clj:5801 clojure.core/trampoline
core.clj:5806 clojure.core/trampoline
RestFn.java:439 clojure.lang.RestFn.invoke
.../git/sicp-clojure/src/sicp_clojure/2_1_exercises_2_24_2_32.clj:186 sicp-clojure.2-1-exercises-2-24-2-32/total-weight*

在优秀的链接之后,使用蹦床的代码是:

代码语言:javascript
复制
(defn total-weight* [mobile]
  (letfn 
    [(branch-weight-cps
      [branch kont]
      (let [structure (branch-structure branch)]
        (if (mobile? (branch-structure branch))
          (do (println "then " structure) (kont (traverse-mobile-cps structure identity)))
          (do (println "else " structure) (kont structure)))))

     (traverse-mobile-cps
      [mobile kont]
      (branch-weight-cps (left-branch mobile)
                         (fn [left-weight]
                           (branch-weight-cps (right-branch mobile)
                                              (fn [right-weight] #(kont (+ left-weight right-weight)))))))]
    (trampoline traverse-mobile-cps mobile identity)))

最后是一些样本数据:

代码语言:javascript
复制
(def branch11 (make-branch 1 1))
(def branch22 (make-branch 2 2))
(def branch36 (make-branch 3 6))
(def branch43 (make-branch 4 3))

(def mobile11-43 (make-mobile branch11 branch43))
(def mobile36-22 (make-mobile branch36 branch22))

(def branch5m1143 (make-branch 5 mobile11-43))
(def branch7m3622 (make-branch 7 mobile36-22))
(def mobile5m1143-7m3622 (make-mobile branch5m1143 branch7m3622))

(total-weight* mobile5m1143-7m3622)

它为什么会爆炸?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-22 10:01:19

按照我的帖子中的相同链接,我已经解决了将我的执行工作提交给:

代码语言:javascript
复制
(defn total-weight* [mobile]
  (letfn
    [(branch-weight-cps
      [branch kont]
      (let [structure (branch-structure branch)]
        (if (mobile? (branch-structure branch))
          (fn [] (traverse-mobile-cps structure kont))
          (fn [] (kont structure)))))

     (traverse-mobile-cps
      [mobile kont]
      (branch-weight-cps (left-branch mobile)
                         (fn [left-weight]
                           (branch-weight-cps (right-branch mobile)
                                              (fn [right-weight] #(kont (+ left-weight right-weight)))))))]
    (trampoline traverse-mobile-cps mobile identity)))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26450982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档