【问题标题】:Multiple lines comments in Scheme (RnRS)Scheme (RnRS) 中的多行注释
【发布时间】:2011-04-13 12:41:30
【问题描述】:

我创建了这个解决方案:

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

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

【问题讨论】:

    标签: scheme comments lisp code-formatting r5rs


    【解决方案1】:

    您不能这样做 - 它不适用于多种情况。以下是一些不起作用的示例:

    (+ (/* 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.
     )
    

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

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

    【讨论】:

    • 非常感谢!所以,我的代码可能对
    • (cons '(/*I'm quoting this/*)a foo)
    • 请给我原始的缺点结构以及您想评论的内容。抱歉,您的代码示例对我来说没有意义。
    • 如果原件是 (cons 'a foo),我想你可以这样做: (cons (/* 'a */ 'temp-value) foo) 如果你想评论 'a
    • (sigh) 仅在为不存在的问题提供较少限制的解决方案时才有意义...如果从实用性的角度考虑,这是一个更糟糕的解决方案看法。众所周知,Scheme 宏系统可以做这样的事情,但众所周知,它非常不方便——所以在语言设计方面,强迫我使用笨拙的构造而不是代码,远非我所说的“优雅” 、“灵活”或“多范式”。
    【解决方案2】:

    MIT、Gnu、R6RS 和 R7RS 支持这样的多行 cmets:

    #|
        This is a comment
     |#
    

    【讨论】:

      猜你喜欢
      • 2012-01-05
      • 1970-01-01
      • 2014-01-15
      • 2015-08-15
      • 2011-09-16
      • 2021-11-27
      • 2011-07-02
      • 2015-08-25
      • 2010-12-03
      相关资源
      最近更新 更多