Live data from Hacker News

Hey, C Is a Functional Language Too

spin.atomicobject.com

71–78 of 78 posts

Re: Hey, C Is a Functional Language Too

#71
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.

I'm not that old but I remember that, back in the days, functional meant just that you are able to use plain functions (without objects) and pass them around. By that definition, C is functional language, as scheme or lisp or javascript and many others. I don't know why, but some years ago, "functional programmers" started to change and twist the definition of functional language step by step, until all those languag…

[deleted]

Re: Hey, C Is a Functional Language Too

#72
it might have also been worthwhile to mention that the stack array declaration:

   int32_t result[my_array_size];
relies on C99 support, which is not necessarily ubiquitous (especially in embedded device work). At the very least, many compilers make you explicitly enable C99 support.

Re: Hey, C Is a Functional Language Too

#74
post #30
post #28

Quick question: why does he do this? int main(int argc, char * argv[]) { (void)argc; (void)argv; I've programmed C while in school, but I don't remember ever seeing this and I'm not sure how to google it.

It stops the compiler from complaining about unused variables: http://stackoverflow.com/questions/8052091/void-cast-of-argc...

Why would you do this instead of declaring it as `int main(void)`?

Re: Hey, C Is a Functional Language Too

#75

Earlier quoted context omitted.

C arrays that live on the stack have to be created with a constant size (at least before C99). It's a limitation of the language regardless of any machine or stack size.

If we're going to grant that a machine can have infinite ram, surely we can grant that a spec of C can have infinite stack allocated arrays... :)

[deleted]

Re: Hey, C Is a Functional Language Too

#76
post #68
post #9

Earlier quoted context omitted.

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.

Re: Hey, C Is a Functional Language Too

#77
post #58

Earlier quoted context omitted.

Sure you can - just not anonymously. Kind of like how you can have "higher order functions" and "closures" in Java with anonymous classes - roughly equivalent, but awkward and not quite what the language was designed for. C is probably better in this regard than Java, but the lack of managed memory is a major drawback.

"just not anonymously" Just not depending on anything in the environment either except globals, which is a huge limitation. So writing a function inc by = \x -> x + by becomes unnecessarily difficult for example.

then you just have a void* / fn ptr couple that you pass around..

Re: Hey, C Is a Functional Language Too

#78
post #25
post #24

Earlier quoted context omitted.

What does proprietary mean in this context? I'm sure it can be copied/implemented by for example gcc if they were interested.

I meant to refer to extensions implemented independently by one compiler or another. Clang and gcc both provide lambdas, but not in the same way, and there's no standard for it. (I could be wrong on both points; this is purely from memory, and could have changed.)

Thanks for your explanation. I had to look and I libblocksruntime which seems to be an open source lib for clang with blocks.
Post reply on HN