【问题标题】:Scheme Programming compilation error方案编程编译错误
【发布时间】:2017-03-10 18:09:23
【问题描述】:

我得到的错误是缺少一个“else”表达式:

(if (not (eq? current_token next_token)) 
    (return #f))

如您所见,我在下图中有 else 语句。只是不知道为什么程序找不到它。

scheme code image

【问题讨论】:

    标签: parsing terminal scheme token racket


    【解决方案1】:

    图片是在问题中包含代码的非常糟糕的方式。对于 每个 if,您需要 3 个额外的元素。 (if predicate consequence alternative)。在您包含的代码中,有(not (eq? current_token next_token)) 形式的谓词,然后您有(return #f),这是结果,但是当谓词为假时,您别无选择。它与cond 中的else 术语无关,因为这是可选的,它是cond 的一部分,而不是if 的一部分,它是cond 后果之一。

    所以当current-tokennext-token 都是评估结果的符号时,我如何看待它。一旦一个术语命中,它将产生后果,而不是访问其他术语。 if 然后检查两者是否相同。如果它们不相同,则会发生错误。

    【讨论】:

      【解决方案2】:

      请注意,if 条件句的语法是:

      (if test-expr then-expr else-expr)
      

      这样如果 test-expr 产生除#f 之外的任何值,然后then-expr 被评估,如果#f 被产生,else-expr 被评估。

      您目前有以下内容:

      (if (not (eq? current_token next_token)) (return #f))
      

      相当于:

      (if test-expr then-expr)
      ; test-expr = (not (eq? current_token next_token))
      ; then-expr = (return #f)
      ; Not complete - missing an else expression
      

      添加else expression 以解决问题。

      例如:

      > (if true 0)      ; test-expr = true, then-expr = 0
      if: missing an "else" expression in: (if true 0)
      > (if true 0 1)    ; test-expr = true, then-expr = 0, else-expr = 1
      0
      > (if false 0 1)   ; test-expr = false, then-expr = 0, else-expr = 1
      1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-10
        • 2016-08-25
        • 2020-09-23
        • 1970-01-01
        • 1970-01-01
        • 2013-05-28
        • 1970-01-01
        相关资源
        最近更新 更多