Live data from Hacker News

Hey, C Is a Functional Language Too

spin.atomicobject.com

11–20 of 78 posts

Re: Hey, C Is a Functional Language Too

#11
post #10

I would say the aspect that defines a functional programming language is the support of higher order functions. I.e. functions that can take functions as arguments and more importantly can return functions as return value.

Doesn't function pointers enable passing functions around in C?

Re: Hey, C Is a Functional Language Too

#12
post #5

Earlier quoted context omitted.

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.

Re: Hey, C Is a Functional Language Too

#16
post #11
post #10

I would say the aspect that defines a functional programming language is the support of higher order functions. I.e. functions that can take functions as arguments and more importantly can return functions as return value.

Doesn't function pointers enable passing functions around in C?

Yes, but you can't combine function pointers to create and return new functions.

Re: Hey, C Is a Functional Language Too

#17
post #10

I would say the aspect that defines a functional programming language is the support of higher order functions. I.e. functions that can take functions as arguments and more importantly can return functions as return value.

Rather than saying that higher-order functions is the defining feature of functional programming, I would say that it's an essential feature to support its object of defining programs in terms of (mathematical) function application rather than procedural state changes.

Re: Hey, C Is a Functional Language Too

#18
post #11
post #10

I would say the aspect that defines a functional programming language is the support of higher order functions. I.e. functions that can take functions as arguments and more importantly can return functions as return value.

Doesn't function pointers enable passing functions around in C?

Yes, but functions aren't truly first-class values; you cannot create new ones on-the-fly (proprietary extensions notwithstanding).

Re: Hey, C Is a Functional Language Too

#19
post #2

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)
Post reply on HN