Hacker News new | past | comments | ask | show | jobs | submit login

No it's not. What makes a language functional is its ability to eliminate tail recursion.



GCC can eliminate tail recursion, so does that make C functional?

I don't think the author is seriously of the belief that C is a functional language. This is just a fun little example of writing C in a functional style.


GCC can even eliminate non-tail recursion, such as with this piece of black magic:

    int factorial(int x) {
       if (x > 1) return x * factorial(x-1);
       else return 1;
    }
will be optimized by GCC to

    int factorial(int x) {
       int result = 1;
       while (x > 1) result *= x--;
       return result;
    }
(http://ridiculousfish.com/blog/posts/will-it-optimize.html)


That's a very weird definition. Eliminating tail calls is fun, and useful. But not all that essential in, say, a lazy language.

Having first-class function values in the first place strikes me as way more important. Purity helps, too.


How do you write a function to sum a list of a billion elements in Haskell?

What would happen without tail call elimination?


With foldl', of course. If you don't have tail recursion elimination, foldl' would have to be provided as a built-in.

Summing up numbers is inherently strict, so you'd want tail call elimination for that. But functional mainstains, like say, map or filter are usually not implemented with tail recursion in Haskell, because that would be too strict and would break on infinite lists.


That's not a very good definition. It means that GHC-flavoured Haskell isn't functional: http://www.haskell.org/haskellwiki/Tail_recursion


I think you might have misinterpreted the contents of that page. As someone who has worked on GHC, I can assure you that it does perform tail call optimisation.


I believe I have. Thank you for correcting me.


Tail-call elimination is a feature of the language environment, not of the language itself.


Not necessarily. The Scheme spec requires tail-call elimination, and I think the same may be true of some other functional-ish languages.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: