简体   繁体   English

和宏修改

[英]And macro modification

(define-macro and
  (lambda args
     (if (null? args) ‪#‎t‬
         (if (null? (cdr args)) (car args)
             (if (car args) `(and ,@(cdr args)) ‪#‎f‬)))))

This is modified and macro, that is different on the last line. 这是经过修改的宏,与最后一行不同。 The correct way I know is correct is 我知道正确的正确方法是

  `(if ,(car args) (and ,@(cdr args)) ‪#‎f‬)))))

But I don't know how that change will affect the behavior of this macro.. I think it works the same, but if it doesn't, can you give an example when it won't? 但是我不知道该更改将如何影响此宏的行为。我认为它的作用相同,但是如果不起作用,可以举个例子说明何时不行吗? enter code here

So you have macro expansion time and you have runtime. 因此,您具有宏扩展时间,并且具有运行时。 Lets imagine that I use your first definition of and with (and (pair? lst) (cdr lst)) . 让我们想象一下,我(and (pair? lst) (cdr lst))使用了and的第一个定义。 The code in effect is (if (car args) `(and ,@(cdr args)) ‪#‎f‬)) and args are ((pair? lst) (cdr lst)) . 有效的代码是(if (car args) `(and ,@(cdr args)) ‪#‎f‬))和args是((pair? lst) (cdr lst)) (car args) in macroexpansion time is (pair? lst) and it's not #f (anything except #f is true) but notice that you are not running (pair? lst) , you are just assuring that I haven't written (and #f something) . 宏扩展时间中的(car args)(pair? lst) ,它不是#f(除了#f以外的任何东西都为true),但是请注意,您没有在运行(pair? lst) ,您只是在确保我没有写过(and #f something) The code running in the function needs to make code and it doesn't have the data at run time rather it has the arguments as represented in source code. 函数中运行的代码需要编写代码,并且在运行时没有数据,而是具有源代码中表示的参数。

By changing your last line to `(if ,(car args) (and ,@(cdr args)) ‪#‎f‬))))) you are no longer checking if the data (pair? lst) is #f but it turns into (if (pair? lst) (and (cdr lst)) #f) and in runtime if will do consequent or alternative based on lst is a pair or not. 通过将最后一行更改为`(if ,(car args) (and ,@(cdr args)) ‪#‎f‬)))))您不再检查数据(pair? lst)是否为#f,但是它会变成(if (pair? lst) (and (cdr lst)) #f)并且在运行时if会基于lst做结果或替代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM