[英]Scheme R5RS: defining let expression
I was wondering how you would go about defining your own let expression in Scheme (I'm very new to scheme). 我想知道您将如何在Scheme中定义自己的let表达式(我对Scheme非常陌生)。 I want it to look like (mylet id expr1 expr2) where id is bound to expr1's value and used in expr2.
我希望它看起来像(mylet id expr1 expr2),其中id绑定到expr1的值并在expr2中使用。 I think it would be something along the lines of:
我认为这可能与以下内容类似:
(define (mylet x a body)
((lambda (x) body) a) )
but that isn't working. 但这不起作用。
When I try 当我尝试
(mylet x 4 (* x 4))
I get the following error: 我收到以下错误:
x: undefined; cannot reference undefined identifier.
What am I doing wrong? 我究竟做错了什么?
let
is actually a macro. let
实际上是一个宏。 You cannot define this as a procedure. 您不能将其定义为过程。 Since you're using Racket, try this:
由于您使用的是球拍,请尝试以下操作:
(define-syntax-rule (mylet x a body)
((lambda (x) body) a))
That looks almost like your original code, but using define-syntax-rule
instead of define
. 看起来几乎像您的原始代码,但是使用
define-syntax-rule
而不是define
。 ;-) That define-syntax-rule
is actually a shortcut for the following full macro: ;-)
define-syntax-rule
实际上是以下完整宏的快捷方式:
(define-syntax mylet
(syntax-rules ()
((_ x a body)
((lambda (x) body) a))))
Indeed, you can even define the "standard" let
macro (minus named let
) this way: 实际上,您甚至可以通过以下方式定义“标准”
let
宏(减去名为let
):
(define-syntax-rule (let ((id val) ...) body ...)
((lambda (id ...) body ...) val ...))
Note the use of the ellipses ( ...
). 请注意省略号(
...
)的使用。 This allows zero or more forms; 这允许零种或多种形式; in this case, it allows
let
to contain multiple body forms. 在这种情况下,它可以让
let
含有多种人体形式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.