0

What is the kernel language representation of N + {Add N - 1} following the stub

local I1 in

// the code

        end
    end
end

the procedure definition of function {Add N} is as follows

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

2 Answers2

2

In the kernel language, you have to define a new identifier for each operation. One for the N-1 operation, another to retrieve the result of the procedure Add and a third one to store the result of N + {Add N-1}. Also you have to declare each local variable separately.

So you get something like this :

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

Then I3 contains the value N + {Add N-1}

demesmaekerf
  • 146
  • 1
  • 6
  • even though your solution seems to work it does not give me the expected answer. To be exact the code body should contain 6 lines (10 lines including stub). –  Nov 16 '15 at 15:38
  • I was able to complete most of the code but missing to lines `local I1 in`\n `local N1 in` `local R1 in` `...` `...` `{Add N1 R1}` `R = N + R1` `end` `end` `end` –  Nov 16 '15 at 15:50
  • Sorry you also have to declare the 1 as a variable in kernel language. All the basic operations have to be done on identifiers, not values. I edited the post – demesmaekerf Nov 16 '15 at 22:16
0

You can get the kernel language of any piece of code with the Mozart IDE.

You juste have to go under the tab Oz > Core Syntax > Whatever piece of code you want translate into KL

It gives

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

It may be hard to read, but the tool is really helpful and powerful