Recursive Functions (58)

1 Name: #!/usr/bin/anonymous : 2008-02-12 09:50 ID:+23K11BA

I am not good at recursive functions and I was wondering if anyone had any tips or examples I could do to get better at it. Right now I am programming in C++ for a class I am taking and would like to get better at it in that language.

2 Name: #!/usr/bin/anonymous : 2008-02-12 10:15 ID:1mWr7Jsm

Try scheme or haskell.
In scheme and haskell the only way to loop is to use recursion.
Read about tail recursion and understand it.

Also, why recursion? Don't use recursion in C++ unless it is the only way.

3 Name: #!/usr/bin/anonymous : 2008-02-12 13:26 ID:Heaven

Forcing recursion where iteration is more logical is silly, and is one reason I'm not too fond of functional programming.

No, what >>1 wants to do is study recursive functions in actual use. Certain algorithms become much easier when implemented recursively, just as others are much easier when implemented iteratively. So to learn properly about recursion, study those cases where it is useful.

Of course, now that I said that I can't think of any good and simple examples off the top of my head. Most of the basic examples of recursion tend to be things which work better iteratively, which makes the whole thing pretty pointless.

I guess traversing trees might be a decent example, but I don't have anything good to link to. So I'll leave the specifics to someone else.

4 Name: >>2 : 2008-02-12 14:02 ID:1mWr7Jsm

>>3
Try to write the fibonacci algorithm iteratively.
The recursive approach is simple
[code]
/* C */
int fib(int n) {

if(n == 0) return 0;
else if(n == 1) return 1;
else return fib(n-1) + fib(n-2);

}

; scheme
(define (fib n)
(cond ((= n 0) 0)

     (= n 1) 1)
(else (fib (- n 1) (- n 2))))))

[/code]

Figuring out how to write it iteratively is not that easy, try it :D

Also, they are not 'forcing' recursion.
Recursion can be the same with iteration if written properly.
(ie where tail-call optimization is possible)

5 Name: #!/usr/bin/anonymous : 2008-02-12 14:03 ID:Heaven

>>4
Ah crap, I forgot about wakabamark

6 Name: #!/usr/bin/anonymous : 2008-02-12 14:23 ID:Heaven

Why is it that whenever recursion comes up, someone invariably gives a terrible fibonacci implementation?

A much more practical example would be directory traversal in a filesystem. Take a quick look at the opendir and readdir functions, and try making a program that finds all the files in a directory and all subdirectories.

Here's a bit of code to get you started:

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
int isdirectory(char *f) {
struct stat s;
return stat(f,&s) == -1 ? 0 : S_ISDIR(s.st_mode);
}

and for reading the directories themselves,

DIR *d;
struct dirent *e;
char *dirname;
d = opendir(dirname);
while ((e = readdir(d))) {
// do stuff with e->d_name here
}
closedir(d);

7 Name: #!/usr/bin/anonymous : 2008-02-12 14:34 ID:Heaven

> Figuring out how to write it iteratively is not that easy, try it :D
int fib(int n)
{
int a = 1, b = 0, t;
while (n-- > 0) {
t = b;
b += a;
a = t;
}
return b;
}

That took me like ten seconds and worked on the first try.

8 Name: #!/usr/bin/anonymous : 2008-02-12 20:19 ID:+wsvkcf7

Seriously, Fibonacci and factorials where exactly what I was referring to when I was saying that "Most of the basic examples of recursion tend to be things which work better iteratively".

That recursive fibonacci implementation is horrible, too. It runs in, what, exponential time? It's exactly that kind of shit that nobody should teach, ever.

This thread has been closed. You cannot post in this thread any longer.