• 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`

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

  • 相关阅读:
    团队冲刺第一天
    leetcode整理
    eclipse 中JSP环境搭建
    java期末小结
    桌面宠物online------------------面对对象程序综合设计2020年
    java
    4.3 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (20分)
    选择
    算法---分支限定0/1背包--蚁群算法
    博客园特效
  • 原文地址:https://www.cnblogs.com/yantt/p/sicp-zhi-ying-yong-xu-he-zheng-ze-xu.html
Copyright © 2020-2023  润新知