【Scheme】元循环求值
#lang scheme
(require rnrs/base-6)
(require rnrs/mutable-pairs-6)
(define (eval exp env)
(cond
((self-evaluating? exp) exp) ;自求值
((variable? exp) (lookup-variable-value exp env)) ;变量
((quoted? exp) (text-of-quotation exp)) ;引用
((assignment? exp) (eval-assignment exp env)) ;赋值
((definition? exp) (eval-definition exp env)) ;定义
((if? exp) (eval-if exp env)) ;if语句
((lambda? exp) ;lambda
(make-procedure (lambda-parameters exp)
(lambda-body exp)
env))
((begin? exp) ;begin
(eval-sequence (begin-actions exp) env))
((cond? exp) (eval(cond->if exp) env)) ;cond
((application? exp)
(apply (eval (operator exp) env)
(list-of-values (operands exp) env)))
(else
(error "Unknown expression type --EVAL" exp))
))
(define (apply procedure arguments)
(cond
((primitive-procedure? procedure)
(apply-primitive-procedure procedure arguments))
((compound-procedure? procedure)
(eval-sequence
(procedure-body procedure)
(extend-environment
(procedure-parameters procedure)
arguments
(procedure-environment procedure))))
(else
(error
"Unknown procedure type --APPLY" procedure))
))
(define (list-of-values exps env) ;以组合式的运算对象为参数,求值各个运算对象,返回这些值的表
(if (no-operands? exps)
'()
(cons
(eval (first-operand exps) env)
(list-of-values (rest-operands exps) env)
)
))
(define (eval-if exp env) ;求值-if语句
(if (true? (eval (if-predicate exp) env)) ;条件为真?
(eval (if-consequent exp) env)
(eval (if-alternative exp) env)))
(define (eval-sequence exps env) ;求值-过程体里的表达式序列
(cond
((last-exp? exps exps) (eval (first-exp exps) env))
(else
(eval (first-exp exps) env)
(eval-sequence (rest-exps exps) env))
))
(define (eval-assignment exp env) ;求值-变量赋值
(set-variable-value! (assignment-variable exp) ;取得变量
(eval (assignment-value exp) env) ;计算值
env)
'ok)
(define (eval-definition exp env) ;求值-变量定义
(define-variable! (definition-variable exp)
(eval (definition-value exp) env))
'ok)
(define (self-evaluating? exp) ;自求值?
(cond
((number? exp) true)
((string? exp) true)
(else false)
))
(define (variable? exp) ;变量?
(symbol? exp))
(define (quoted? exp) ;引号表达式?
(tagged-list? exp 'quote))
(define (text-of-quotation exp) (cadr exp)) ;获取引号表达式引用的值
(define (tagged-list? exp tag) ;获取list的tag
(if (pair? exp)
(eq? (car exp) tag)
false))
(define (assignment? exp) ;赋值语句?
(tagged-list? exp 'set!))
(define (assignment-variable exp) (cadr exp)) ;获取赋值表达式的被赋值变量
(define (assignment-value exp) (caddr exp)) ;获取赋值表达式要赋的值
(define (definition? exp) (tagged-list? exp 'define)) ;定义语句?
(define (definition-variable exp) ;获取定义式的变量
(if (symbol? (cadr exp))
(cadr exp)
(caddr exp)))
(define (definition-value exp) ;获取定义式的值
(if (symbol? (cadr exp)) ;如果是变量,说明为(define <var> <value>)形式
(caddr exp)
(make-lambda
(cdadr exp) ;lambda参数
(cddr exp) ;lambda函数体
)))
(define (lambda? exp) (tagged-list? exp 'lambda)) ;lambda?
(define (lambda-parameters exp) (cadr exp)) ;lambda的参数
(define (lambda-body exp) (caddr exp)) ;lambda的函数体
(define (make-lambda parameters body) ;lambda构造函数
(cons 'lambda (cons parameters body)))
(define (if? exp) (tagged-list? exp 'if))
(define (if-predicate exp) (cadr exp)) ;获取if语句的条件式
(define (if-consequent exp) (caddr exp)) ;获取if为真时的表达式
(define (if-alternative exp)
(if (not (null? (cdddr exp))) ;if为假时是否存在表达式
(cadddr exp) ;存在
'false)) ;不存在,返回'false
(define (make-if predicate consequent alternative) ;if构造函数
(list 'if predicate consequent alternative))
(define (begin? exp) (tagged-list? exp 'begin))
(define (begin-actions exp) (cdr exp))
(define (last-exp? seq) (null? (cdr seq))) ;为list中最后一个exp?
(define (first-exp seq) (car seq))
(define (rest-exps seq) (cdr seq))
(define (sequence->exp seq) ;序列转变为一个表达式
(cond
((null? seq) seq)
((last-exp? seq) (first-exp seq))
(else (make-begin seq)) ;将一个表达式序列转变为begin表达式
))
(define (make-begin seq) (cons 'begin seq))
(define (application? exp) (pair? exp))
(define (operator exp) (car exp)) ;(operator <pr1> <pr2> ...)
(define (operands exp) (cdr exp))
(define (no-operands? ops) (null? ops)) ;不含有参数?
(define (first-operand ops) (car ops))
(define (rest-operands ops) (cdr ops))
(define (cond? exp) (tagged-list? exp 'cond))
(define (cond-clauses exp) (cdr exp))
(define (cond-else-clause? clause) ;是否为cond中的else
(eq? (cond-predicates clause) (cdr clause)))
(define (cond-predicates clause) (car clause)) ;clause的条件部分
(define (cond-actions clause) (cdr clause)) ;clause的结果部分
(define (cond->if exp) ;cond->if
expand-clauses (cond-clauses exp))
(define (expand-clauses clauses)
(if (null? clauses)
'false
(let ((first (car clauses))
(rest (cdr clauses)))
(if (cond-else-clause? first)
(if (null? rest)
(sequence->exp (cond-actions first))
(error "ELSE clause isn't last --COND->IF"
clauses))
(make-if (cond-predicates first)
(sequence->exp (cond-actions first))
(expand-clauses rest))))))
(define (true? x)
(not (eq? x false)))
(define (false? x)
(eq? x false))
(define (make-procedure parameters body env) ;过程构造
(list 'procedure parameters body env))
(define (compound-procedure? p)
(tagged-list? p 'procedure))
(define (procedure-parameters p) (cadr p))
(define (procedure-body p) (caddr p))
(define (procedure-environment p) (cadddr p))
(define (enclosing-environment env) (cdr env)) ;外围环境
(define (first-frame env) (car env))
(define the-empty-environment '())
(define (make-frame variables values)
(cons variables values))
(define (frame-variables frame) (car frame))
(define (frame-values frame) (cdr frame))
(define (add-binding-to-frame! var val frame)
(set-car! frame (cons var (car frame)))
(set-cdr! frame (cons val (cdr frame))))
(define (extend-environment vars vals base-env)
(if (= (length vars) (length vals))
(cons (make-frame vars vals) base-env)
(error "Wrong Num of arguments supplied" vars vals)))
(define (lookup-variable-value var env)
(define (env-loop env)
(define (scan vars vals)
(cond ((null? vars)
(env-loop (enclosing-environment env))) ;当前frame无所找变量,则去外围环境寻找
((eq? var (car vars)) (car vals))
(else (scan (cdr vars) (cdr vals)))))
(if (eq? env the-empty-environment)
(error "Unbound variable" var)
(let ((frame (first-frame env)))
(scan (frame-variables frame)
(frame-values frame)))))
(env-loop env))
(define (set-variable-value! var val env)
(define (env-loop env)
(define (scan vars vals)
(cond ((null? vars)
(env-loop (enclosing-environment env)))
((eq? var (car vars))
(set-car! vals val))
(else (scan (cdr vars) (cdr vals)))))
(if (eq? env the-empty-environment)
(error "Unbound variable -- SET!" var)
(let ((frame (first-frame env)))
(scan (frame-variables frame)
(frame-values frame)))))
(env-loop env))
(define (define-varialbe! var val env)
(let ((frame (first-frame env)))
(define (scan vars vals)
(cond ((null? vars)
(add-binding-to-frame! var val frame))
((eq? var (car vars))
(set-car! vals val))
(else (scan (cdr vars) (cdr vals)))))
(scan (frame-variables frame)
(frame-values frame))))
(define primitive-procedures ;基本过程
(list (list 'car car)
(list 'cdr cdr)
(list 'cons cons)
(list 'null? null?)
(list '+ +)
(list 'display display)))
(define (primitive-procedure-names) ;获取所有基本过程的名称
(map car primitive-procedures))
(define (primitive-procedure-objects)
(map (lambda (proc) (list 'primitive (cadr proc)))
primitive-procedures))
(define (define-variable! var val env)
(let ((frame (first-frame env)))
(define (scan vars vals)
(cond ((null? vars)
(add-binding-to-frame! var val frame))
((eq? var (car vars))
(set-car! vals val))
(else
(scan (cdr vars) (cdr vals)))))
(scan (frame-variables frame)
(frame-values frame))))
(define (setup-environment)
(let ((initial-env
(extend-environment(primitive-procedure-names)
(primitive-procedure-objects)
the-empty-environment)))
(define-variable! 'true true initial-env)
(define-variable! 'false false initial-env)
))
(define (primitive-procedure? proc) ;为基本过程?
(tagged-list? proc 'primitive))
(define (primitive-implementation proc) (cadr proc))
(define (apply-primitive-procedure proc args) ;应用一个基本过程
(apply-in-underlying-scheme
(primitive-implementation proc) args))
(define apply-in-underlying-scheme apply)
(define input-prompt ";;;M-Eval input:")
(define output-prompt ";;; M-Eval value:")
(define (drive-loop) ;循环
(prompt-for-input input-prompt)
(let ((input (read)))
(let ((output (eval input the-global-environment)))
(announce-output output-prompt)
(user-print output)))
(drive-loop)
)
(define (prompt-for-input string)
(newline)
(newline)
(display string)
(newline))
(define (announce-output string)
(newline)
(display string)
(newline))
(define (user-print object)
(if (compound-procedure? object)
(display (list 'compound-procedure
(procedure-parameters object)
(procedure-body object)
'<procedure-env>))
(display object)))
(define the-global-environment (setup-environment)) ;全局环境
(drive-loop)
【Scheme】元循环求值的更多相关文章
- 高斯消元与行列式求值 part1
两道模板题,思路与算法却是相当经典. 先说最开始做的行列式求值,题目大致为给一个10*10的行列式,求其值 具体思路(一开始看到题我的思路): 1.暴算,把每种可能组合试一遍,求逆序数,做相应加减运算 ...
- 用for和while循环求e的值[e=1+1/1!+1/2!+1/3!+1/4!+1/5!+...+1/n!]
/*编敲代码,依据下面公式求e的值. 要求用两种方法计算: 1)for循环.计算前50项 2)while循环,直至最后一项的值小于10-4 e=1+1/1!+1/2!+1/3!+1/4!+1/5!+. ...
- 延迟求值-如何让Lo-Dash再提速x100?
「注释」作者在本文里没有说明这么一个事实: 目前的版本Lo-Dash v2.4.1并没有引入延迟求值的特性,Lo-Dash 3.0.0-pre中部分方法进行了引入,比如filter(),map(),r ...
- 编写高质量代码改善C#程序的157个建议[匿名类型、Lambda、延迟求值和主动求值]
前言 从.NET3.0开始,C#开始一直支持一个新特性:匿名类型.匿名类型由var.赋值运算符和一个非空初始值(或以new开头的初始化项)组成.匿名类型有如下基本特性: 1.既支持简单类型也支持复杂类 ...
- 基于上三角变换或基于DFS的行(列)展开的n阶行列式求值算法分析及性能评估
进入大一新学期,看完<线性代数>前几节后,笔者有了用计算机实现行列式运算的想法.这样做的目的,一是巩固自己对相关概念的理解,二是通过独立设计算法练手,三是希望通过图表直观地展现涉及的两种算 ...
- 求值器本质--eval&apply
最近跟着(How to Write a (Lisp) Interpreter (in Python))使用python实现了一个简易的scheme解释器.不得不说使用python这类动态语言实现不要太 ...
- [SICP] 求值规则
在Java语言学习中,通常不太关注求值规则. (2+4*6)*(3+5+7)这样的组合式的求值规则.通常归结为优先级问题: if.for等的求值规则通常归结为语义. 函数式编程语言的Scheme,将这 ...
- python迭代器实现斐波拉契求值
斐波那契数列(Fibonacci sequence),又称黄金分割数列,也称为"兔子数列":F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2,n∈N*).例 ...
- POJ 2106 Boolean Expressions (布尔表达式求值)
题意:关于!,&,| 的运算,表达式中V代表true,F代表false. 思路:见代码吧,很详细了. 要注意 !!!F,!(...) 的情况. #include <iostream> ...
随机推荐
- pom格式
参考: https://www.jianshu.com/p/0e3a1f9c9ce7 https://blog.csdn.net/u012152619/article/details/51485297 ...
- demo: 全页面CSS3动画的一个参考例子
全页面CSS3动画的一个参考例子: http://wow.blizzard.cn/wow/wod-achievement/ 魔兽的一个活动页 第二页.第三页,文字进入页面 <script src ...
- css:蓝环章鱼
许多海洋生物色彩艳丽,这次用css仿制一下蓝环章鱼的蓝环 <script type="text/javascript" src="http://cdn.bootcs ...
- spark 数据读取与保存
spark支持的常见文件格式如下: 文本,json,CSV,SequenceFiles,Protocol buffers,对象文件 1.文本 只需要使用文件路径作为参数调用SparkContext 中 ...
- 42. oracle通过两张表的一个字段对应,update其中一张表的某个字段
update A a set a.A2 = (select b.B2 from B b where b.B1=a.A1) where exists (select 1 from B where B.B ...
- 【Flex】自定义组件-combobox组件
1包结构 2 Test.mxml <?xml version="1.0" encoding="utf-8"?> <s:Application ...
- Flex验证器 validate stringvalidate
1 validate <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns: ...
- beego生成 api 项目 && api 文档
目标 生成 api 项目,并且自动生成db的mapper.module等:同时生成api文档 操作步骤 1.生成 api 项目,并且自动生成db全表的映射 bee api [projectName] ...
- 爬虫--Scrapy-参数等级和请求传参
日志等级 日志等级(种类): ERROR:错误 WARNING:警告 INFO:一般信息 DEBUG:调试信息(默认) 指定输入某一中日志信息: settings:LOG_LEVEL = ‘ERROR ...
- Python 基础补充(一) 列表、元组、集合、字典的区别和相互转换
一.列表.元组.集合.字典的区别 列表 元组 集合 字典 英文 list tuple set dict 可否读写 读写 只读 读写 读写 可否重复 是 是 否 是 存储方式 值 值 键(不能重复) ...