【问题标题】:Kernel language representation of a function函数的内核语言表示
【发布时间】:2015-11-16 13:44:26
【问题描述】:

在存根之后N + {Add N - 1} 的内核语言表示是什么

local I1 in

// the code

        end
    end
end

函数{Add N}的过程定义如下

proc {Add N R}
    if N == 0 then R = 0
    else N + {Add N - 1}
    end
end

【问题讨论】:

    标签: oz


    【解决方案1】:

    在内核语言中,您必须为每个操作定义一个新标识符。一个用于N-1 操作,另一个用于检索过程Add 的结果,第三个用于存储N + {Add N-1} 的结果。您还必须分别声明每个局部变量。

    所以你会得到这样的东西:

    local I1 in
       local I2 in
          local I3 in
             I1 = 1
             I2 = N - I1
             {Add I2 I3}
             R = N + I3
          end
       end
    end
    

    那么I3包含值N + {Add N-1}

    【讨论】:

    • 即使您的解决方案似乎有效,但它并没有给我预期的答案。确切地说,代码主体应包含 6 行(包括存根在内的 10 行)。
    • 我能够完成大部分代码,但缺少行 local I1 in\n local N1 in local R1 in ... ... ... {Add N1 R1} R = N + R1 end5@9876543 end
    • 对不起,您还必须在内核语言中将 1 声明为变量。所有基本操作都必须在标识符上完成,而不是值。我编辑了帖子
    【解决方案2】:

    您可以使用 Mozart IDE 获取任何代码段的内核语言。

    你只需要进入选项卡 Oz > 核心语法 > 任何你想翻译成 KL 的代码

    它给了

    declare Add in
    local UnnestApply1 UnnestApply2 in
       proc {Add N Result1}
          local IfArbiter1 UnnestApply3 in
         UnnestApply3 = 0
         IfArbiter1 = N == UnnestApply3
         if IfArbiter1 then
            Result1 = 0
         else
            local UnnestApply4 UnnestApply5 UnnestApply6 in
               UnnestApply6 = 1
               UnnestApply5 = N - UnnestApply6
               {Add UnnestApply5 UnnestApply4}
               Result1 = N + UnnestApply4
            end
         end
          end
       end
       UnnestApply2 = 4
       {Add UnnestApply2 UnnestApply1}
       {Browse UnnestApply1}
    end
    

    可能很难阅读,但该工具确实很有帮助且功能强大

    【讨论】:

      猜你喜欢
      • 2011-04-20
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      相关资源
      最近更新 更多