SICP之应用序和正则序

以一个题目来说明

(define (square x) (* x x))
(define (sum-squares x y) 
  (+ (square x) (square y)))
(define (f a)
   (sum-squares (+ a 1) (* a 2)))  

列如求 (f 5)

应用序

应用序则是先一步步替换子组合求值再去应用的为应用序(“evaluate the arguments and then apply”翻译为“先求参数值再应用”)

`(f 5)`展开
`(sum-squares (+ a 1) (* a 2)))`
`5`替换 `a`为
`(sum-squares (+ 5 1) (* 5 2)))`
直接进行子项求值
`(+ 5 1)`求值为`6`,`(* 5 2)`求值`10` 替换后为 `(sum-squares 6 10)`
替换`sum-squares`
`(+ (square 6) (square 10))`
替换`square`
`(+ (* 6 6) (* 10 10))`
最后求和 `136`
上面的过程可以看出是一步步传入参数值去计算子项后最后得出结果

正则序

正则序则是一步步替换所有过程直至最原始的符号(“fully expand and then reduce”翻译为完全展开后归约)

`(f 5)`展开
`(sum-squares (+ 5 1) (* 5 2)))`展开
`(+ (square (+ 5 1)) (square (* 5 2)))`展开
`(+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2)))`最后直接归约求和 `136`

可以看出正则就是把所有过程函数全部替换调直到最原始的一些过程函数(+ * / 这些)最后归约。

原文地址:https://www.cnblogs.com/yantt/p/sicp-zhi-ying-yong-xu-he-zheng-ze-xu.html