5 ms·
> Ignore all that. Functional code is characterised by one thing: the absence of side effects. It doesn’t rely on data outside the current function, and it does
by rsp1984 12y ago
> Ignore all that. Functional code is characterised by one thing: the absence of side effects. It doesn’t rely on data outside the current function, and it doesn’t change data that exists outside the current function. Every other “functional” thing can be derived from this property.
This. So much this. I'd go even further and say functional programming is just like imperative programming but without variables (except for those passed in as arguments). This has always struck me as the key difference between the two. The rest (first class / higher order functions, mapping, reducing, pipelining, recursing, currying etc) in my opinion is just making things more convenient in functional languages, dealing with this absence of variables.
The key achievement of functional programming in my opinion is then the provision of special tools (see above) that help programmers solve particular problems in particular efficient ways, that programmers would have solved by using variables in a possibly less efficient way in an imperative language. In short functional programming forces the user to pick the right tool for the task.
- angersock 12y agoEh, I think that leaving out first-class functions is unforgivable. The complete spirit of functional programming can be neatly summed up as: "Functional programming is the creation of useful programs using only functions mapping input arguments (perhaps themselves functions!) to outputs (also perhaps functions!) without external state." You can't leave out the function part of functional programming.
- gracenotes 12y agoI would agree with GP that emergent code organization principles that come from eliminating side effects are the crux of FP, as far as being the bottom line of what makes it effective and worth internalizing. Higher-order functions are a simple and powerful form of abstraction, but not much else. I could imagine something which has the software engineering benefits of an FP language without HOF, although maybe it wouldn't culturally be one. I think getting enthusiastic about higher-order functions and trying to use them in OO languages can range from good to neutral to bad. On the plus side, code is usually shorter. On the negative side, it's not necessarily more readable, and types still usually expose the same amount of information about their inputs/outputs, which is "call me, anything can happen" (plus plenty of opportunity for lazy side effects, the most confusing kind).
- angersock 12y agoSo, I see what you're saying, but I maintain that without first-order functions, you can't do anything useful with code organization. If you wanted to simulate that, imagine writing in C or C++ or Javascript using only functions, only returning copies of things changed by the function, and never referencing global state. There's nothing particularly magic about that, other than better testability--it's just really slow and obnoxious imperative code. When you start passing around functions and currying values, though, then you're actually starting to see cool things happening.
- jacquesm 12y agoI think you're making this too abstract to be understood without having already seen the light, perhaps an example would help?
- angersock 12y agoOkay, sure, let's look at it in Javascript. The task? Take a list of numbers, and produce a list containing only the squares of those numbers--and only those squares falling between 10 and 50. var inList = [1,2,3,4,5,6,7,8,9,10]; var non_fp = function ( myListOfNumbers ) { var out = []; var temp = []; var i = 0; var j = 0; // square the numbers for (i = 0; i < myListOfNumbers.length; i++) { temp.push( myListOfNumbers[i] * myListOfNumbers[i] ); } for (j = 0; j < temp.length; j++) { if ( temp[j] > 10 && temp[j] < 100 ) { out.push( temp[j] ); } } return out; } var fp = function ( myListOfNumbers ) { return myListOfNumbers.map( function (x) { return x*x; } ) .filter( function (x) { return (x > 10) && (x< 100); }); } The first has no side effects. The second version has no side effects, and wouldn't be possible without first-class functions, and to me is clearly the more functional answer.
- jacquesm 12y agoSuggested minor change to the non_fp one: var inList = [1,2,3,4,5,6,7,8,9,10]; var non_fp = function ( myListOfNumbers ) { var out = []; // square the numbers for (var i = 0; i < myListOfNumbers.length; i++) { var x = myListOfNumbers[i] * myListOfNumbers[i]; if (x > 10 && x < 100) { out.push( x ); } } return out; }
- SeanDav 12y agoI am probably making a similar point to "gracenotes" here, but as I see it the functions in functional programming are simply a tool that can be used in order to achieve side effect free programming. They are not what really defines functional programming as a concept, even though they share the root name "function".
- stonemetal 12y agoHow many functional programming languages do you cross of the list of functional programming languages if you make that claim? At the very least anything Lisp related, all the ML languages as well. You are pretty much left with Miranda, Haskell, and a few related languages.
- coolsunglasses 12y ago>You are pretty much left with Miranda, Haskell, and a few related languages. Agda and Idris as well. The bar for what constitutes "FP" has been changing since the 50s. Doesn't sound unreasonable to me.
- seanmcdirmid 12y agoFunctional programming used to mean programming with first class functions and closures; they even put it in the name "functional"al programming (just like OOP meant program with objects, whether this involved classes or not). So that is Lisp, the first FP in the 60s, and then the MLs of the 70s/80s. They knew about purity, and even preferred it (for things like list comprehensions, it is very useful), but were never very dogmatic about it.
- rtpg 12y agoAn FP language doesn't preclude non-FP code, just like OOP doesn't preclude ... procedural code? I'm not sure what the opposite would be but it exists (think Matlab code). I would argue that the lisps are not very functional languages (both in the importance of effects and the style of code I've seen written in lisps), but Scala provides for a lot of FP assurances (val over var, good FP-y libs), and people in the community value effectlessness. So I'm comfortable calling Scala a FP language.
- agumonkey 12y agoWould concatenative languages apply ?
- nbevans 12y agoYep. It always amazes me when die-hard OOP'ers get it all wrong and think FP is at war with their beloved OO. Well no. Plenty of FP languages exist that combine FP and OO together. Scala.. F#.. OCaml.. etc.
- seanmcdirmid 12y agoIt always amazes me when die-hard FP'ers get it all wrong and think OOP is at war with their beloved FP. Well no, plenty of OOP languages exist that combine FP and OO together. Scala.. C#.. Clojure.. etc.
- nbevans 12y agoPerfect example ^
- mercurial 12y agoC# at least doesn't do immutability per default, which is another classical property of FP.
- seanmcdirmid 12y agoMost functional programming languages don't do immutability by default (which anyways, is an aspect of the library that can choose to be or not to be; LINQ definitely is). There are also no true scotts men.