简体   繁体   中英

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)) . The code in effect is (if (car args) `(and ,@(cdr args)) ‪#‎f‬)) and args are ((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) . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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