4 ms·
I'm not sure if I like this definition of "functional". Don't get me wrong, I'm a Haskell guy and think Haskell is fantastic for production use, but this defin
by mightybyte 11y ago
I'm not sure if I like this definition of "functional". Don't get me wrong, I'm a Haskell guy and think Haskell is fantastic for production use, but this definition of "functional" almost seems to rule out everything except Haskell. Even if I try to maximize my Haskell fanboyism I can't with a straight face choose a definition for "functional programming language" that of the today's production-viable languages only includes Haskell. A definition that narrow just isn't very useful. I don't have super strong opinions about how it should be defined, but I tend to be drawn towards a more fuzzy definition. One that seems attractive is: "functional programming languages are languages that focus on functions as the basic unit of abstraction".
- typon 11y agoThe wiki definition is on point in my opinion: "a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions".
- cygx 11y agoI explained it like this elsewhere: The functional programming paradigm models computation as a relation between sets, and is thus inherently declarative. However, in practice, we often think of functions as imperative, ie you put in an input value and get out an output value, same as with a procedure. From this point of view, the characteristic property of a function is that it has no side-effects. Because of ambiguity of the terms, we call such a function pure, and a language which only has pure functions would be a purely functional language. However, not all functional languages are pure: A functional language is a language with syntax and semantics which allows the programmer to use the functional paradigm efficiently. Some of the concepts which make using the paradigm feasible include - among others - lambda expressions with lexical closure, higher-order functions, variant types and pattern matching, lazy evaluation, type-inference (in case of statically-typed languages). This is by no means an authorative list, and a language can very well be functional without providing all or even most of them, but if a language does - ie makes them usable without having to jump through major hoops - their presence is a strong indicator that the language should be considered functional.
- seanwilson 11y agoIt's a spectrum really. For example, OCaml is much more functional than JavaScript by having strong typing, inductive types, pattern matching etc. but lets you write functions with side effects unlike Haskell.
- mightybyte 11y agoHaskell lets you do side effects. The difference is that the type system carefully controls them.
- seanwilson 11y agoI was more referring to how OCaml allows mutable references and arrays, and allows IO similar to imperative languages whereas Haskell isn't so generous.
- wyager 11y agoHaskell actually does allow mutability in pure code using the ST monad. It's pretty cool; you can use ST to have mutable variables and arrays and such, but the mutable values aren't allowed to escape the ST "context". So you're still guaranteed immutability outside of the ST monad, because the type system locks all mutable values inside the monad. This is mostly only used in very-high-performance libraries that use algorithms based on array access.
- mightybyte 11y agoHaskell absolutely allows mutable references and arrays. See IORef [1] and MVector [2]. Notice how the functions that perform mutation are monadic. That's Haskell controlling the side effects and making sure that they don't pollute pure code. The ST monad stuff [3] that wyager mentioned is even cooler because it allows you to bundle up side effecting stuff and actually use it in pure code in a completely safe way, which is something that you can't do (without the dubious usafePerformIO) with the IO monad. [1] https://downloads.haskell.org/~ghc/latest/docs/html/libraries/base-4.8.1.0/Data-IORef.html https://downloads.haskell.org/~ghc/latest/docs/html/librarie... [2] http://hackage.haskell.org/package/vector-0.11.0.0/docs/Data-Vector-Mutable.html#g:10 http://hackage.haskell.org/package/vector-0.11.0.0/docs/Data... [3] https://downloads.haskell.org/~ghc/latest/docs/html/libraries/base-4.8.1.0/Control-Monad-ST.html https://downloads.haskell.org/~ghc/latest/docs/html/librarie...
- jarcane 11y agoI tend to think of it in that way as well. To me, "functional programming" taken back to origins in Lisp and the lambda calculus is a style of programming that focuses on the composition of functions. The "purity" element is merely the consequence of this focus: in order to be composable, our functions largely need to be free of side effects, and take and return values as mathematical functions do. I think the emphasis on function composition also makes for a better, more believable sales pitch, once you take into account first-class functions as values and the kinds of abstractions and patterns they allow. I have a version of this pitch which goes like this: Imagine a function that, given the right inputs, could produce any output in the known universe. To which you might rightly answer "that's pure nonsense". The sheer volume of internal logic would naturally be more complicated than everything within that universe. But if that function can itself receive other functions as inputs, then the potential outputs expand almost infinitely. And indeed, this is why the lambda calculus itself has proved Turing-complete: simply by composing functions within functions in this way, you can literally recreate the whole of computing and mathematics. In practice though, people have their limits, and the day to day operation of a computer does require occasionally breaking the perfect mathematical puzzle for the sake of getting work done, and the vast majority of languages in existence allow for exactly that. A definition that doesn't allow for them might well be "true" in the "pure" sense of the word, but it's not very useful for people other than mathematicians. And hell, even the Turing-machine uses mutable state.
- Avshalom 11y agoYeah I mean I for one am over ten years old so I remember when 'functional' pretty much just meant functions-as-first-class-values and every-thing-returns-a-value. Then because those are pretty reasonable language features all the languages that had been written in the late 80s-mid 90s and became popular around 2000 had those features. So then for a while 'functional' was just sort of way of saying 'not java'. Now 'functional' seems to mean strong algebraic typing for some reason.
- jacquesm 11y ago> I'm a Haskell guy Don't ever be a 'programming language 'x' guy or girl'. Be yourself, know some tools and skills and be prepared to switch those out for whatever works best in that particular application. If you limit yourself to a certain programming language, no matter how great it seems to be today you've built in your own obsolescence. Better to be a guy or a girl with x years of Haskell experience and a whole bunch of other stuff besides.
- draw_down 11y agoOn the one hand I think that's reading too much into the phrase "Haskell guy", but on the other I disagree in the sense that being a dilettante is no good either. I think people should be "T-shaped", deep on one to a few languages/platforms and some experience with many others. Spending a long time with one language/platform will teach you deeper lessons over time than switching every 6 months.
- mightybyte 11y agoI used that phrase as a more concise way of communicating pretty much what you are suggesting. I assumed that since my choice of X was Haskell it was pretty much a given that I haven't limited myself to a certain programming language and aggressively investigate promising new technologies.
- jberryman 11y agoWhere "function" means "like in math", right? Not "arbitrary callable block of code". I think it makes sense to think of "functional" as more or less synonymous with "approaching Haskell", even if you want to include many other languages under the umbrella.