Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have 4 different commands in LOOP programming language:

    y=Zero()
    y=Val(x)=copy x and put it in register y
    y=Inc(x)=x+1
    y=Dec(x)=x-1

Finally I also have

    loop n times {
    ...
    }

How is it possible that there is an equivalent loop program to every other loop program which uses only Inc, Zero and the loop?

For me it is not clear how to express the Dec-command using only Inc, Zero and the loop.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Here's one way to code y=Dec(x):

y=Zero();
z=Zero();
loop x times {
  loop z times {
    y=Inc(y);
  }
  z=Zero();
  z=Inc(z);
}
share|improve this answer
    
Thank you. I am little bit confused concerning the double loop. What exactly means. z=Zero() loop x times { loop y times { z=Inc(z) } } Does this describe a function g(x,y)=x+y ? –  Voyage Mar 10 '13 at 14:58
    
The inner loop in my code just does y=y+z. Your example says “increase z y times (i.e., z=z+y), and do that x times, (i.e., z=z+x*y).” –  Christopher Creutzig Mar 10 '13 at 16:11
    
Oh, and in case wonder why my code performs Dec: In the first iteration of the outer loop, z is zero, so the inner loop does nothing. In the remaining x-1 iterations, z=1, so y starts at 0 and is increased x-1 times. –  Christopher Creutzig Mar 10 '13 at 16:14
    
...and how do you obtain a negative number using this method? If X = 0, the result will not be -1. –  Mason Wheeler Mar 10 '13 at 17:51
    
Loop doesn't have negative numbers. x=Zero(); x=Dec(x); makes x equal zero, according to the language web page (and the wikipedia page linked above). Otherwise, you're of course right. –  Christopher Creutzig Mar 10 '13 at 20:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.