【发布时间】:2015-01-27 03:18:02
【问题描述】:
我正在观看 SICP 的视频讲座。目前我正在研究 4A 模式匹配和基于规则的替换。
到目前为止,我发现 Matcher 和 Instantiator 很简单。但我无法进入简化器。
(define (simplifier the-rules)
(define (simplify-exp exp)
(try-rules (if (compound? exp)
(map simplify-exp exp)
exp)))
(define (try-rules exp)
(define (scan rules)
(if (null? rules)
exp
(let ((dict (match (pattern (car rules))
exp
(empty-dictionary))))
(if (eq? dict 'failed)
(scan (cdr rules))
(simplify-exp (instantiate (skeleton (car rules)) dict))))))
(scan the-rules))
simplify-exp)
我在这里看到了关于这个主题的另一个问题,它根据pair? 定义了compound?。但是,那simplify-exp 喂给try-rules 什么呢?
【问题讨论】:
-
您使用的是球拍还是计划?问题标记为scheme,但您的代码以
#lang racket开头。它们是相似语言,但它们并不相同。在 Scheme 和许多 Lisps 中,空列表 是 一个原子,因此((null? s) '())(您在答案中添加的内容)在((atom ?s) s)之后是多余的。