4 ms·
This article is another one of those that confuses functional programming with pure programming (which is, I believe, by definition also functional). You can d
by tomp 15y ago
This article is another one of those that confuses functional programming with pure programming (which is, I believe, by definition also functional).
You can do a lot of functional programming in a language where values are mutable. I use it all the time in python: the map and filter functions, list comprehensions, zip, ... For me, functional programming means having first-class functions that can be passed to/from other functions. A standard library of common functional combinators help, but you can always write your own.
Funny, but some of the most often mentioned functional languages, such as OCaml and Scala, allow liberal use of mutable variables. They just make it a bit harder/more explicit, so the programmer has to give more consideration into it.
- jshen 15y agoYou must have read a different article than the one I read. "Functional programming is a practical implementation of Alonzo Church's ideas. Not all lambda calculus ideas transform to practice because lambda calculus was not designed to work under physical limitations. Therefore, like object oriented programming, functional programming is a set of ideas, not a set of strict guidelines. There are many functional programming languages, and most of them do many things very differently. In this article I will explain the most widely used ideas from functional languages" No where does he say that functional programming is pure programming.
- sharmajai 15y agoYou can do a lot of functional programming in a language where values are mutable. Sure you can do whatever you want, but if the language does not support things like tail call optimization, non-imperative function definition, writing code in functional style turns out to be a lot slower (due to non-tail-optimized function call overhead) and less safe (due to potential side effects in function definition) than if you are writing it in a functional language with explicit support for these facilities.
- JadeNB 15y agoThat's true, but this functionality is perfectly compatible with mutability. Various flavours of Lisp have been in the forefront (CMA: among 'popular' languages) in making this functionality available, but I don't know of any (CMA: 'popular') pure flavour of Lisp. (Maybe Clojure --but its preference for 'immutable by default' is quite different from not having mutable values at all!)
- zby 15y agoOK - I checked wikipedia: In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. Sounds exactly what I was thinking - map is not mathematical function - because it can interact with the global state (you can apply map twice to the same data and get different results - for example if in the function you are mapping you use 'time') - and thus using it is not 'functional programming'. If your first-class functions were mathematical functions - then we could agree - but map is not one.
- tomp 15y agoI'm reading this differently than you are; > functional programming is a programming paradigm NOT a programming language. What I think this means is that your usage of map is such that the computation is what matters, not mutating the state. I can do that just as well in Python, JavaScript, OCaml, Scala, Clojure and Haskell. In other languages too, yes, but not nearly as easily (due to lack of closures/complicated functional types/no first-class functions/...). If you were to insist that functional programming is just math (no mutable state), then even Haskell and Mathematica are far from functional.
- zby 15y agoI insist that what is important is how much of the purity you have - because it is a more widely used definition (as supported by my wikipedia quote) and also this is what implies the nice computational properties of the code, and what is not important is if you use first class functions (not in the mathematical sense).
- polymatter 15y ago"map is not mathematical function - because it can interact with the global state (you can apply map twice to the same data and get different results" I don't think I quite understand you. I think when you say "mathematical function" you mean "pure function". And when you say "global state" I assume you mean "impure operations". But (from my understanding) map is purely functional and doesn't do anything impure like IO. Its purely a mathematical mapping between a collection of input values to a collection of output values, based on applying a function to each input value in turn. You seem to be suggesting "map f (map f x)" (that is applying map twice to the same data) is not the same value as "map f x" and therefore its not "mathematical".