• (2 Pages)
• 1
• 2

## 24 Replies - 1482 Views - Last Post: 12 April 2009 - 12:55 PM

### #16 JonBernal

Reputation: 3
• Posts: 199
• Joined: 14-March 09

Posted 10 April 2009 - 06:35 AM

haha

Well, I guess we all know who's getting
old and not dealing with the future! D:

hahaha jk

Well idk, I'm still waiting on my book, its
now 2 days late. Which is frustrating...
But I'm hoping something good out of this,
I'm excited

### #17 KYA

• g++ jameson.cpp -o beverage

Reputation: 3145
• Posts: 19,185
• Joined: 14-September 07

Posted 10 April 2009 - 06:10 PM

NeoTifa, on 10 Apr, 2009 - 06:21 AM, said:

Seriously, though, recursion makes my brain explode. I prefer iteration. That way I can see wtf is going on in every step.

That really doesn't make sense. Recursion is iteration of itself. Using a print statement either way shows what's "going on".

### #18 baavgai

• Dreaming Coder

Reputation: 6200
• Posts: 13,313
• Joined: 16-October 07

Posted 11 April 2009 - 04:00 PM

I think the problem with recursion is it's often taught as "advanced." It's applied to complex problems where recursion is most applicable, like divide and conquer. The standard "towers of hanoi" solution, while I understand it, still makes my brain hurt and gives me serious dyslexic flashes trying to trace it.

Recursion isn't hard, it's just not as linear as iteration. Here's an example of recursion that's basically iterative.
```// print n..1
void iterPrint(int n) {
while(n>0) {
printf("%d ", n);
n--;
}
}

// print n..1
void recurPrint(int n) {
if (n>0) {
printf("%d ", n);
recurPrint(n-1);
}
}

```

Here's an example, going the other direction. In this, recursion gets to work for us. Note how almost nothing has changed in the recursive version.

```// print 1..n
void iterPrintFwd(int n) {
int i;
for(i=1; i<=n; i++) {
printf("%d ", i);
}
}

// print 1..n
void recurPrintFwd(int n) {
if (n>0) {
recurPrintFwd(n-1);
printf("%d ", n);
}
}

```

Recursion offers possibility of one of those "eureka" moments in programming. If you understand how those two functions work, you'll be ready for more complex problems.

### #19 Martyr2

• Programming Theoretician

Reputation: 4674
• Posts: 12,777
• Joined: 18-April 07

Posted 11 April 2009 - 04:23 PM

Just to add onto what baavgai said about being taught as "advanced" it is also being taught in situations where it shouldn't be. Recursion for a Fibonacci sequence for instance is a bad idea. The same process could be done with an iterative loop which is faster because of lacking the overhead of a function call.

But having said that, recursion has its place in specialized situations. I find it great for tree traversals etc. But it should be limited to those special cases where it actually adds value.

As for the OP.... just another language as usual. The world has too many of them as it is.

### #20 JonBernal

Reputation: 3
• Posts: 199
• Joined: 14-March 09

Posted 11 April 2009 - 05:33 PM

Mm, Just an other language then?

### #21 baavgai

• Dreaming Coder

Reputation: 6200
• Posts: 13,313
• Joined: 16-October 07

Posted 11 April 2009 - 08:06 PM

JonBernal, on 11 Apr, 2009 - 06:33 PM, said:

Mm, Just an other language then?

They all are.

I have to heartily agree with Martyr, Fibonacci simply awful for recursion. That said, for a functional language, it many not be. The guts functional languages tend to optimized the sting out of recursion. Otherwise, they would honestly suck.

I felt guilty about the code above. For the sake of completeness, and discussion, I wanted to show a Haskell example But, well, I didn't know Haskell. Took a bit, a true Haskell expert may call it drek; I'm cool with that. In Haskell, the recursive number lister might look like this:

```recurPrintFwd n | n<=0 = [] | otherwise = recurPrintFwd (n-1) ++ show n ++ " "

```

### #22 JonBernal

Reputation: 3
• Posts: 199
• Joined: 14-March 09

Posted 11 April 2009 - 09:13 PM

haha, OCD Maybe?

### #23 baavgai

• Dreaming Coder

Reputation: 6200
• Posts: 13,313
• Joined: 16-October 07

Posted 12 April 2009 - 04:22 AM

JonBernal, on 11 Apr, 2009 - 10:13 PM, said:

haha, OCD Maybe?

Lol, hardly. If so, my desk would have much less crap on it. More OPD, where the P is Programming or maybe Puzzle.

The reason some languages exist at all is that programmers enjoy programming. Given a new toy, the first question is can it do all the stuff the old toy did. What does it do better, what worse.

People write programs to solve problems, but there is a satisfaction in the writing. People don't sit down to solve a jigsaw puzzle just because they want to see what it looks like when they're finished.

In the end, the real reason to learn a new computer language you have no ready use for is entertainment. Because you can. It's not really a bad reason.

### #24 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

Posted 12 April 2009 - 07:42 AM

i agree with baavgai and Martyr2, recursion is great for some problems, tree traversals using iterations are painful to the programmer, a lot of code to write and harder to think of.

### #25 JonBernal

Reputation: 3
• Posts: 199
• Joined: 14-March 09

Posted 12 April 2009 - 12:55 PM

Haha

Very true man.
I love programming for that simple reason
that when i'm having trouble programming
something and then i get it to work
It just feels awesome