[英]Using a list as argument names for lambda scheme/racket
I'm working on a scheme evaluator in scheme. 我正在研究Scheme中的方案评估器。 I need to implement let, I have parsed so that I have variable names, values to input and the body of the function. 我需要实现let,我已经对其进行了解析,以便拥有变量名,要输入的值和函数的主体。 I need to return a lambda function using the parsed information and as such I have the following code: 我需要使用已解析的信息返回一个lambda函数,因此我有以下代码:
(define (eval-let exp env)
((lambda (let-variables (let-bindings exp)) (let-body exp)) (let-exp (let-bindings exp))))
(let-variables (let-bindings exp)) evaluates to a list of variable names (ex: '(xy)), so I'm basically evaluating to this: (let-variables(let-bindings exp))评估为变量名列表(例如:'(xy)),所以我基本上是在评估:
((lambda '(x y) (* x y)) '(2 3))
The scheme interpreter simple says: #%plain-lambda: not an identifier in: (let-bindings exp) which I'm guessing is because it wants a set of identifiers, not a list of values. 方案解释器简单地说:#%plain-lambda:不是(:let-bindings exp)中的标识符,我猜这是因为它想要一组标识符,而不是值列表。
How do I turn my list of values into a set of identifiers? 如何将值列表转换为一组标识符?
To implement a let
expression in your own interpreter first you have to transform it into a lambda
application, something similar to this (procedure names should be self-explanatory): 要首先在自己的解释器中实现let
表达式,您必须将其转换为lambda
应用程序,与此类似(过程名称应不言自明):
(define (let->combination exp)
(let* ((bindings (let-bindings exp))
(body (let-body exp))
(exps (bindings-all-exps bindings))
(lambda (make-lambda (bindings-all-vars bindings) body)))
(make-application lambda exps)))
(define (make-lambda parameters body)
(list* 'lambda parameters body))
(define (make-application proc . args)
(cond ((null? args) (list proc))
((list? (car args)) (cons proc (car args)))
(else (cons proc args))))
And after the syntax transformation has been performed you can proceed to evaluate it: 在完成语法转换之后 ,您可以继续对其进行评估:
(eval (let->combination exp) env)
What I'm trying to point out is that you shouldn't try to evaluate it directly. 我要指出的是,您不应该尝试直接对其进行评估。 Also be careful, the code you're generating has a couple of incorrect quotes: 另外请注意,您生成的代码有两个不正确的引号:
((lambda '(x y) (* x y)) '(2 3))
^ ^
here here
It should look like this instead: 它应该看起来像这样:
((lambda (x y) (* x y)) 2 3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.