Clojure核心与变量匹配错误
我正在尝试使用具有core/match和core/typed的clojure解决SICP练习 我在听珍妮·阿德基森的演讲 到目前为止,我得到的是:Clojure核心与变量匹配错误,clojure,clojure-core.typed,clojure-core.match,Clojure,Clojure Core.typed,Clojure Core.match,我正在尝试使用具有core/match和core/typed的clojure解决SICP练习 我在听珍妮·阿德基森的演讲 到目前为止,我得到的是: (ns sicp.chapter2_ex29 (:require [clojure.core.typed :as t] [clojure.core.match :refer [match]])) ; Type definitions (t/defalias Mobile '{:left Branch, :right Br
(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]}}]
(ns sicp.chapter2_ex29
(:require[clojure.core.typed:as t]
[clojure.core.match:参考[match]])
; 类型定义
(t/defalias Mobile'{:左分支,:右分支})
(t/defalias重量编号)
(t/defalias结构)
(t/U'[(t/值:重量)重量]
“[(t/Value:mobile)mobile]))
(t/defalias分支{:长度编号,:结构})
; 建设者
(t/ann使移动[分支机构->移动])
(defn使移动[左右]
{:左:左:右})
(t/ann制造分支[编号结构->分支])
(定义制造分支[长度结构]
{:长度:结构})
; 吸气剂
(t/ann左分支[移动->分支])
(defn左分支[移动]
(:左移动))
(t/ann右分支[移动->分支])
(defn右分支[移动]
(:右移动))
(t/ann分支长度[分支->编号])
(定义分支长度[分支]
(:长度分支)
(t/ann分支结构[分支->结构])
(defn分支机构[分支机构]
(:结构科)
; 总重
(t/ann总重量[手机->号码])
(defn总重量[移动式]
(t/letfn>[结构重量:-[结构->数量]
(结构重量[结构]
(做
(println(str“结构->”结构)
(println(str“structure0->”(获取结构0)))
(println(str“structure1->”(get structure 1)))
(匹配结构)
[重量]重量
[:移动手机](移动总重量)))
分支重量:-[分支->编号]
(树枝重量[树枝]
(结构权重(分支结构分支)))]
(让
[左(分支重量(左分支移动))
右(分支权重(右分支移动))]
(做
(println(str“left->”left))
(println(str“right->”right))
(+左右(()()))
(t/ann mobile1 Mobile)
(def mobile1(使移动
(制作支管3[:重量4])
(制作分支5[:权重2]))
(总重量1) 问题似乎是中的标识符mobile
(match structure
[:weight weight] weight
[:mobile mobile] (total-weight mobile))))
。。。已作为封闭函数定义中的参数绑定:
(defn total-weight [mobile]
... )
将匹配
表单更改为
(match structure
[:weight w] w
[:mobile m] (total-weight m))))
。。。删除错误
规则似乎是:
如果匹配
模式中的名称已绑定,则不会在本地反弹。信息技术
被解释为其普遍价值
我得说我只是偶然发现了这个错误。我更希望本地绑定优先,但事实并非如此
注释
match
表达式的语法不符合要求,这需要
(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)))