你不能这样做——它在很多情况下都不起作用。以下是一些不起作用的示例:
(+ (/* foo */) 1 2)
(define (foo a (/* b */) c) ...)
(/* foo; bar */)
(/*x*/)
(let ((x (/* 1 */) 2))
...)
(let ((/* (x 1) */)
(x 2))
...)
(car '((/* foo */) 1 2 3))
直到R5RS的Scheme报告中都没有标准的多行注释,但是R6RS添加了一种无论如何都被广泛使用的语法:#|...|#
.
但如果你really想要...
这就是我在评论中谈论的内容:如果您愿意将整个代码包装在宏中,那么宏可以处理整个代码体,这在更多上下文中都有效。几乎所有这些,除了尝试注释掉语法上无效的内容,例如上面的分号示例或未终止的字符串。你可以自己判断是否真的值得付出努力......
(就我个人而言,尽管我很喜欢这类游戏,但我仍然认为它们毫无意义。但如果你真的喜欢这些游戏and你认为它们有用,然后看看下面的作业部分......)
(define-syntax prog (syntax-rules () [(_ x ...) (prog~ (begin x ...))]))
(define-syntax prog~
(syntax-rules (/* */)
[(prog~ (/* x ...) b ...)
;; comment start => mark it (possibly nested on top of a previous mark)
(prog~ (x ...) /* b ...)]
[(prog~ (*/ x ...) /* b ...)
;; finished eliminating a comment => continue
(prog~ (x ...) b ...)]
[(prog~ (*/ x ...) b ...)
;; a comment terminator without a marker => error
(unexpected-comment-closing)]
[(prog~ (x0 x ...) /* b ...)
;; some expression inside a comment => throw it out
(prog~ (x ...) /* b ...)]
[(prog~ ((y . ys) x ...) b ...)
;; nested expression start => save the context
(prog~ (y . ys) prog~ ((x ...) (b ...)))]
[(prog~ (x0 x ...) b ...)
;; atomic element => add it to the body
(prog~ (x ...) b ... x0)]
[(prog~ () prog~ ((x ...) (b ...)) nested ...)
;; nested expression done => restore context
(prog~ (x ...) b ... (nested ...))]
[(prog~ () /* b ...)
;; input done with an active marker => error
(unterminated-comment-error)]
[(prog~ () b ...)
;; all done, no markers, not nested => time for the burp.
(b ...)]))
举个例子:
(prog
(define x 1)
(display (+ x 2)) (newline)
/*
(display (+ x 10))
/* nested comment! */
(/ 5 0)
*/
(define (show label /* a label to show in the output, before x */
x /* display this (and a newline), then returns it */)
(display label)
(display x)
(newline)
x
/* this comment doesn't prevent the function from returning x */)
(let ([x 1] /* some comment here */ [y 2])
(show "result = " /* now display the result of show... */
(show "list = " (list x /* blah blah */ y)))
'done /* just a value to return from the `let' expression */)
(show "and ... " '(even works /* boo! */ inside a quote))
)
Homework
为了获得额外的积分,请扩展它,以便您可以注释掉不平衡的括号。例如,让这个工作:
(prog
blah blah /* junk ( junk */ blah blah /* junk ) junk */ blah blah.
)
显然,输入作为一个整体应该有平衡的括号——这意味着实现这种扩展没有多大意义。即使没有它,注释掉不平衡的括号有什么意义呢?
但如果有人一路走到这里,那么你一定很享受这种自虐……对吧?