scheme - Scheme 中的多行注释 (RnRS)

标签 scheme comments lisp code-formatting r5rs

我创建了这个解决方案:

; use like this:
; (/* content ... */ <default-return>)
; or
; (/* content ... */) => #f
(define-syntax /*
  (syntax-rules (*/)
    ((/* body ... */) #f)
    ((/* body ... */ r) r)))

但这真的是最好或最简单的方法吗?

最佳答案

您不能这样做——它不适用于许多上下文。以下是一些不起作用的示例:

(+ (/* 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 添加了一个无论如何都被广泛使用的语法:#|...|#

但是如果你真的想要...

这就是我在评论中所说的内容:如果您愿意将整个代码包装在一个宏中,那么宏就可以处理整个主体,这在更多上下文中都可以有效。除了试图注释掉语法上无效的内容(如上面的分号示例或未终止的字符串)之外,几乎所有这些都是。你可以自己判断是否真的值得付出努力......

(就个人而言,尽管我很喜欢这类游戏,但我仍然认为它们毫无意义。但是如果你真的很喜欢这些游戏并且你认为它们很有用,那么请看下面的作业部分...)

(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))

 )

作业

要获得额外的功劳,请扩展它以便您可以注释掉不平衡的括号。例如,使这个工作:

(prog
 blah blah /* junk ( junk */ blah blah /* junk ) junk */ blah blah.
 )

显然,输入作为一个整体应该有平衡的括号——这意味着实现这种扩展没有多大意义。即使没有它,注释掉不平衡的双亲有什么意义?

但如果有人一路走到这里,那你一定很享受这种 self 折磨吧……对吧?

关于scheme - Scheme 中的多行注释 (RnRS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3732173/

相关文章:

lambda - 在 Racket 中使用 lambda 进行迭代?

python-3.x - 如何在没有额外注释的情况下从 Jupyter Notebook 下载

scheme - 在 Racket 方案或其他方案中解码十六进制格式字符串的惯用方法

Emacs/普通口齿不清 : Quote variable's value

方案递归(十进制到八进制)

functional-programming - 如何在 Dr.Racket 中重新分配值

macros - 如何编写 MIT Scheme 宏来返回 lambda 形式?

visual-studio-code - Visual Studio Code 中取消注释的快捷方式是什么?

pycharm - 如何在 PyCharm 注释中添加删除线?

lisp - 如何在 Lisp 格式函数中打印空白字符?