Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can you be more precise about the difference between a program written in the functional style, and one which is written using functional programming?


Well there is the "official" definition on Wikipedia.[0]

Informally, my definition of a functional programming language is one that supports functional style as the default. (Hooray for using a recursive definition. :p) The language should strive for referential transparency and the language and compiler should be optimized for functional idioms.

Let's define the sum of a list functionally[1]:

    # Python
    sum = lambda x: reduce(int.__add__, x, 0)

    # Haskell
    sum = foldl' (+) 0
You could write this same function in C using function pointers and arrays. However it's not very convenient to do so. You would need to write a reduce function, an add function, anonymous functions don't exist, and syntactically it would be very messy. On top of all this, the compiler is not optimized for reduce functions vs an imperative for loop.[2]

More generally, functional style typically refers a style of programming where most functions are pure and side effects are contained specifically. This allows for easier reasoning of a program's correctness.

John Carmack has a post about functional programming in C++:

http://www.altdevblogaday.com/2012/04/26/functional-programm...

Summed up, functional style is about containing side effects and functional programming has built-in support for common idioms.

[0]: https://en.wikipedia.org/wiki/Functional_programming

[1]: This ignores the fact that sum() is already provided in most functional languages.

[2]: Python straddles my definition of functional programming and that's why it's awesome and frustrating at the same time. It supports many staple functional idioms but is not optimized for it. Function calls are very expensive, and thus recursive performance is also hurt tremendously.

Plus Guido hates functional programming:

http://python-history.blogspot.com/2009/04/origins-of-python...




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

Search: