gpt4 book ai didi

Clojure核心与变体匹配错误

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

我正在尝试解决this使用带有核心/匹配和核心/类型的 clojure 进行 SICP 练习。

我有点关注 Jeanine Adkisson 的 "Variants are Not Unions"讲话。

这是我到目前为止得到的:

(ns sicp.chapter2_ex29
(:require [clojure.core.typed :as t]
[clojure.core.match :refer [match]]))

; Type definitions
(t/defalias Mobile '{:left Branch, :right Branch})

(t/defalias Weight Number)

(t/defalias Structure
(t/U '[(t/Value :weight) Weight]
'[(t/Value :mobile) Mobile]))

(t/defalias Branch '{:length Number, :structure Structure})

; Constructors
(t/ann make-mobile [Branch Branch -> Mobile])
(defn make-mobile [left right]
{:left left :right right})

(t/ann make-branch [Number Structure -> Branch])
(defn make-branch [length structure]
{:length length :structure structure})

; Getters
(t/ann left-branch [Mobile -> Branch])
(defn left-branch [mobile]
(:left mobile))

(t/ann right-branch [Mobile -> Branch])
(defn right-branch [mobile]
(:right mobile))

(t/ann branch-length [Branch -> Number])
(defn branch-length [branch]
(:length branch))

(t/ann branch-structure [Branch -> Structure])
(defn branch-structure [branch]
(:structure branch))

; Total weight
(t/ann total-weight [Mobile -> Number])
(defn total-weight [mobile]
(t/letfn> [structure-weight :- [Structure -> Number]
(structure-weight [structure]
(do
(println (str "structure -> " structure))
(println (str "structure0 -> " (get structure 0)))
(println (str "structure1 -> " (get structure 1)))
(match structure
[:weight weight] weight
[:mobile mobile] (total-weight mobile))))
branch-weight :- [Branch -> Number]
(branch-weight [branch]
(structure-weight (branch-structure branch)))]
(let
[left (branch-weight (left-branch mobile))
right (branch-weight (right-branch mobile))]
(do
(println (str "left ->" left))
(println (str "right ->" right))
(+ left right)))))

(t/ann mobile1 Mobile)
(def mobile1 (make-mobile
(make-branch 3 [:weight 4])
(make-branch 5 [:weight 2])))

(total-weight mobile1) ; <- works as expected = 6

(t/ann mobile2 Mobile)
(def mobile2 (make-mobile
(make-branch 3 [:weight 4])
(make-branch 5 [:mobile (make-mobile
(make-branch 2 [:weight 3])
(make-branch 4 [:weight 2]))])))

(total-weight mobile2) ; <- throws java.lang.IllegalArgumentException: No matching clause: [:mobile {:left {:length 2, :structure [:weight 3]}, :right {:length 4, :structure [:weight 2]}}]

Structure 类型是一个变体:它要么是一个权重,它只是一个数字(它的形式为 [:weight weight],其中 weight 是一个数字),要么它是一个移动设备(它具有 [:mobile mobile] 的形式,其中移动是移动类型的东西)。

它类型检查和总权重功能适用于一个简单的输入:一个由两个权重组成的移动设备。

但是正如您所看到的,对于具有移动设备分支的移动设备来说,它会失败。

由于某种原因,它与 [:mobile mobile] 案例不匹配。知道我在做什么错吗?

最佳答案

问题似乎是标识符 mobile

(match structure
[:weight weight] weight
[:mobile mobile] (total-weight mobile))))

... 已经绑定(bind)为封闭函数定义中的参数:
(defn total-weight [mobile]
... )

更改 match形成
(match structure
[:weight w] w
[:mobile m] (total-weight m))))

... 消除错误。

规则似乎是:

If a name in a match pattern is bound, it is not rebound locally. It is interpreted as its prevailing value.



我不得不说我只是偶然发现了错误。我希望本地绑定(bind)优先,但事实并非如此。

备注
match 的语法表达式不跟随 the documentation ,这将需要...
(match [structure]
[[:weight w]] w
[[:mobile m]] (total-weight m))

...但是您的缩写版本也同样有效。

Clojure 习惯用法是尽可能使用标准数据结构——尤其是映射——避开访问函数和构造函数。这样,我们可以写出类似
; Total weight

(declare structure-weight)

(t/ann total-weight [Mobile -> Number])
(defn total-weight [mobile]
(match [mobile]
[{:left bl, :right br}]
(apply + (map
(comp structure-weight :structure)
[bl br]))))

(t/ann structure-weight [Structure -> Number])
(defn structure-weight [structure]
(match [structure]
[[:weight w]] w
[[:mobile m]] (total-weight m)))

关于Clojure核心与变体匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31680028/

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