Scheme(RnRS)中的多行注释

Fel*_*ipe 4 lisp scheme comments code-formatting r5rs

我创建了这个解决方案

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

但它真的是最好还是最简单的方法?

Dav*_*ske 6

MIT、Gnu、R6RS 和 R7RS 支持多行注释,如下所示:

#|
    This is a comment
 |#
Run Code Online (Sandbox Code Playgroud)


Eli*_*lay 5

你不能这样做 - 它不适用于许多情境.以下是一些不起作用的示例:

(+ (/* 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))
Run Code Online (Sandbox Code Playgroud)

计划报告中没有标准的多行注释直到R5RS,但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 ...)]))
Run Code Online (Sandbox Code Playgroud)

一个例子:

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

 )
Run Code Online (Sandbox Code Playgroud)

家庭作业

要获得额外的功劳,请将其扩展,以便您可以评论不平衡的问题.例如,使这项工作:

(prog
 blah blah /* junk ( junk */ blah blah /* junk ) junk */ blah blah.
 )
Run Code Online (Sandbox Code Playgroud)

显然,输入作为一个整体应该有平衡的parens - 这意味着实现这种扩展没有多大意义.即使没有它,评论一个不平衡的人也有什么意义?

但如果有人一路走到这里,那么你必须享受这种自我折磨......对吗?

  • (*叹气*)只有为不存在的问题提供一种不那么有限的解决方案才有意义......如果你从实用的角度考虑它,这是一个更糟糕的解决方案.众所周知,Scheme宏系统可以做这些事情,但众所周知它非常不方便 - 所以在语言设计方面,迫使我使用笨拙的结构代替代码远远不是我标记为"优雅"的东西,"灵活的"或"多范式". (2认同)