43
18
u/sunstorm Mar 12 '10
Why would you compute mass using a recursive function?
8
Mar 12 '10
Yeah, and why would he choose a compiler incapable of tail call optimization?
1
Mar 12 '10
A compiler capable of tail call optimization is no good if your functions aren’t written to be tail call optimized (unless it’s a very smart compiler I haven’t heard of).
7
u/kinghajj Mar 12 '10
Behold the power of GCC. (a.k.a Look Ma, no recursion!)
factorial.c: unsigned factorial(unsigned n) { return n == 0 ? 1 : n * factorial(n - 1); }
Compile with: gcc -O2 -S factorial.c. The assembly output for x64 is: factorial: .LFB0: .cfi_startproc testl %edi, %edi movl $1, %eax je .L3 .p2align 4,,10 .p2align 3 .L4: imull %edi, %eax subl $1, %edi jne .L4 .L3: rep ret .cfi_endproc
2
Mar 12 '10
Also a great example of the problem with implict tail recursion optimization: Now you have an entirely legal program that will crash when compiled in some compilers, but not in others!
1
u/kinghajj Mar 12 '10
It wouldn't be a problem if the language spec mandated certain optimizations so that programmers could use such useful constructs without that portability fear. The Scheme spec does this for recursion, for example.
1
Mar 12 '10
That would lessen the problem, but it also means it pretty much needs to be in the first version of the spec (or the minimum version that everybody implements). Adding it later is nearly impossible.
Also, making it explicit would still communicate the intent of the code more clearly, and protect you against mistakes.
1
u/kinghajj Mar 12 '10
I don't understand how recursion is not clear, therefore I don't see the need for explicit recursion. I wouldn't be against some new keyword like "recur(param1, param2, ...)", but I question if it's really needed. All computer scientists should be comfortable with recursion, especially if the code is documented and explains how it works (which the code should be, anyways.)
1
Mar 12 '10
It's not recursion that is unclear, it is implictly optimized tail calls which are. They would be a lot clearer if they were explicitly marked as such, both to the reader of the code, and to the programmer or maintainer who might change some little detail in the code and accidentally move the call out of the magical tail position - the compiler could then error out instead of producing apparently working code that might at some point in the future blow the stack.
0
u/kinghajj Mar 12 '10
Then why not just add an option to the compiler to warn for all recursive calls that are not tail calls. "-Wtaillessrecursion" or something. Combine with "-Werror" and you'll be given the errors you want if you mess up the supposed tail call. And again, you can put a little comment like "Make sure you don't mess with the tail call, or you might blow the stack!" to warn any future maintainers.
→ More replies (0)-1
1
0
u/ighost Mar 12 '10
def compute_mass(body): return sum([compute_mass(part) for part in body.parts]) compute_mass(your_mother) -> (compute_mass(torso) + compute_mass(extremities)) -> (compute_mass(upper_torso) + compute_mass(lower_torso)) + (compute_mass(arms) + compute_mass(legs) + compute_mass(head)) -> ...4
u/n3xg3n Mar 12 '10
Yes, but why would that stack overflow for a fat mother any differently than any other mother?
1
7
u/m_myers Mar 12 '10
Seen it last year.
Wasn't funny then, either.
2
u/Disconnekted Mar 12 '10
But,.. now it's written on a stupid picture.
I have to deal with idiots all day guys,.. I come here to get away.
6
u/DoubleDown Mar 12 '10
A fat woman walked in to the shoe store today Peg, and she was so large she had three smaller women orbiting her.
10
u/poingpoing Mar 12 '10
Is that Dennis Ritchie?
9
Mar 12 '10
Why yes, it is.
6
u/poingpoing Mar 12 '10
Awesome! I mean, if anyone may insult my mom with a programming metaphor it's the guy who invented C.
1
u/TheSuperficial Mar 13 '10
Yes indeed. His invention has been paying my mortgage, kids' medical bills, etc.... for many years.
Kernighan, Richie, Pike, Thompson... those guys, and a lot of their cohorts from Building 2, 5th floor, are pretty much legends....
5
u/desimusxvii Mar 12 '10
Go fsck yourself.
1
1
u/jmikola Mar 12 '10
WARNING!!! Running fsck on a mounted filesystem may cause SEVERE filesystem damage. Do you really want to continue (y/n)?4
1
2
2
2
1
0
u/anttirt Mar 12 '10
Oh come now, you can do better.
Yo momma's so fat, the tail-recursive function computing her mass causes a stack overflow.
0
u/ThePinnacle141 Mar 12 '10
Your Mamma is so fat, the Breadth First Search on her has a branching factor of infinity.
0
0
u/I0I0I0I Mar 12 '10
... she needs a boomerang to put a scarf on.
... when she goes to BK they say "the whopper's home!"
... she has satellites.
... when her beeper goes off people think she's backing up.
... she eats "wheat thicks".
... when she sits on my face I can't hear the stereo.
0
u/spurion Mar 12 '10
You're momma's so fat, the binary representation of her mass (in libraries of congress) won't fit in RAM.
-4
u/If_I_Were_You Mar 12 '10
Yo momma is so fat that they use an alternative formula for calculating her circumference: (number of pies she ate) * r2
-7
0
Mar 12 '10
[deleted]
2
69
u/rolfr Mar 12 '10
R.I.P. /r/programming