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.

Regarding turing completeness, i read that for a language/machine to be turing complete it is required that it has some sort of conditional.

Consider the factorial problem, we would typically define the algorithm as

Solution 1.
int fact(int n)
{
if n == 0 then
 return 1;
else
 return n*fact(n-1);
}

An alternative would be to define the function fact as

Solution 2.

int fact(int n)
{
 if n == 0 then return 1;
 elseif n == 1 then return 1;
 elseif n == 2 then return 2;
 elseif n == 3 then return 6;
 ....
 all cases define as above
}

Also, another solution would be to have functions like:

Solution 3.

int fact0()
{
return 1;
}

int fact1()
{
return 1*fact0;
}

int fact2()
{
return 2*fact1;
}

int fact3()
{
return 3*fact2;
}

Then we would have an array of function pointers p which we would have to index according to the factorial we want to determine, such that p[0] points to fact0, p[1] points to fact1, and so forth.

I know that this might sound dumb, but it actually works! What makes solution 1. a valid argument to show that conditionals are required to compute the factorial?

Some additional thoughts on my part: 1.Is solution 1 the only true "algorithm"? Because the other 2 solutions to not seem to "compute" anything 2.Are conditionals required only because we don't know the results (or execution steps) of functions beforehand?

share|improve this question

closed as off-topic by delnan, Doval, Euphoric, Telastyn, 9000 Feb 3 at 20:50

  • This question does not appear to be about software development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Again? Didn't you ask this question twice already? What's wrong with the existing answers? –  delnan Feb 3 at 20:36
    
Solutions 2 and 3 won't work for any arbitrary input. –  Euphoric Feb 3 at 20:37
5  
I'm voting to close this question as off-topic because it belongs to Computer Science, and has in fact already been posted there. –  delnan Feb 3 at 20:38

2 Answers 2

I'm not sure where you get the idea from that conditionals are required. λ-calculus doesn't have conditionals, yet it is Turing-complete.

share|improve this answer
    
you could simply vote close this question as a duplicate of one you answered before –  gnat Feb 3 at 20:52
1  
@gnat: Actually, all of those OISCs have a conditional. –  Jörg W Mittag Feb 3 at 21:02

It is possible to use dynamic dispatch in some cases. Rather than using a conditional, delegate to an object or one of its subclasses.

Conditional code:

if (a) {
  // Do one thing
}
else {
  // Do something else
}

This could become the following with some refactoring:

a.doSomething();

Of course, the dynamic dispatch mechanism is a form of conditional (think vtable), and the code that assigns a may be conditional as well.

My gut feeling is that a program without conditionals is essentially deterministic and might not be very interesting for many applications, but proving that is undecidable.

Related reading:

share|improve this answer
    
"It is possible to use dynamic dispatch in some cases." – in all cases, actually. Smalltalk doesn't have conditionals (nor loops) at all. It purely uses dynamic dispatch. The familiar ifThenElse does exist, but purely as a library feature: Boolean has two subclasses, TrueClas and FalseClass, both classes have an ifThenElse method which takes two arguments, TrueClass's implementation ignores the second argument, FalseClass's ignores the first. –  Jörg W Mittag Feb 3 at 20:45
    
I admit I am not familiar with Smalltalk but I think my points about conditionals being required at some level is valid. I would be utterly shocked if the machine code being executed did not have conditional jump instructions. –  Snowman Feb 3 at 20:52
    
I'm not disagreeing. Ad-hoc polymorphism is a form of conditional execution. One that completely subsumes if/else, which is why it always works. (That's why it doesn't make sense to have if in an OO language. You are just replicating a language feature that already exists in a more powerful form anyway.) –  Jörg W Mittag Feb 3 at 21:05

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