6 ms·
I think it's time we stop using the phrase "functional" to describe this paradigm; at this point, every imperative language made in the past 30 years has first-
by kibwen 6y ago
I think it's time we stop using the phrase "functional" to describe this paradigm; at this point, every imperative language made in the past 30 years has first-class functions, and there is no other first-class language feature common to all the self-described "functional" languages.
IMO, a more accurate term would be stateless programming. This paradigm is about minimizing state. Of course, as the OP mentions, state is often quite useful. But minimizing state, especially global state, particularly mutable state, and especially particularly global mutable state, is something even imperative language fans can get behind.
- Avshalom 6y agoIt's a bit more subtle but the lack of statements that is, everything returns a value, is an important feature/quality.
- heavenlyblue 6y agoExpressional or equational programming
- gnulinux 6y agoFunctional languages do have statements. a : Int -> Int a x = x + 2 both `:` and `=` are statements, not expressions.
- fiddlerwoaroof 6y agoThis is sometimes true, the `=` equivalent in Clojure is an expression, though: user=> (def a (def b 2)) user=> a #'b Lisps generally only have expressions (every form returns a value.
- gnulinux 6y agoLisp runtime implicitly runs each top-level expression as statement. When you run (def x y) it's not only true that this expression returns 'x, it's also true that it alters the static state of the program by adding 'x to the scope. To make a purely statementless language, I think you need to make everything happen in a single monadic computation. E.g. in pseudo-Haskell-ish: main : Scope -> StaticIO Scope main scope0 = do scope1, x' <- def scope0 "x" Int scope2, xv <- dec scope1 "x" 3 But the main part is still statement.
- tsimionescu 6y agoAt least in all of the discussions I have seen, the difference between statements and expressions is whether they return a value, not whether they have side-effects. So (def x b) is an expression, because it returns a value. {} in C is a statement, even though it has no side effects. Now, there are some statements in CL as far as I remember - I believe (declaim x integer) is a kind of statement, at least in the sense above. But you can write a CL program that doesn't use a single statement.
- fiddlerwoaroof 6y agoThe return value of some forms is implementation dependent, but all forms return a value in CL: CL-USER(1): (defparameter *foo* (declaim (special x))) *FOO* CL-USER(2): *foo* (X) CL-USER(3): (defparameter *bar* (declaim (type integer x))) *BAR* CL-USER(4): *bar* (X) In other cases, e.g. `(values)` which is an expression that returns no values, the value is nil
- fanf2 6y agoThey are usually called declarations rather than statements, I think.
- deleted 6y ago[deleted]
- jhgb 6y agoHow do any of those qualify as statements? Statements represent actions in imperative programming languages. Declarations and definitions are not actions.
- dustingetz 6y agoI think it's called functional because it emerges from the lambda calculus (truly just lambdas at the bottom) – but note this definition excludes javascript-with-immutability as well as clojure, not sure if lisp counts
- pyrale 6y agoThey all count. It's like an athletics competition: everyone is an athlete even if not all people compete in the same discipline, or reach the same performances.
- dsabanin 6y agoFunctional programming was never about first-class functions. It was always about programming with pure and total functions in their mathematical version.
- acdha 6y agoThis may be true in your personal experience but that’s certainly not universal. A key distinction is when you learned this: 2-3 decades ago when most people were using languages without first-class functions, this was very commonly the most important feature mentioned because it was the most pronounced difference. That’s no longer emphasized as much now that this feature and related techniques has become mainstream.
- pyrale 6y agoI believe the statement GP made has been true since the days of Church. It is not surprising that "what's the difference between your language and mine" will yield an answer that is different than what you'll get if you ask "what is that concept you're trying to build a language after?"
- acdha 6y agoThat might be pointing out an important distinction: did you learn this formally from that academic community or elsewhere? As a global assertion it’s wrong but it could be accurately representative if you’re only talking about subcommunities in the former case.
- pyrale 6y agoI have made another comment in this thread where I make a distinction between what people _try_ to achieve, and what tradeoffs they make on the way. You are right that most functional languages do not have pure and total functions. However, the inspiration they take from is usually the pure/total functions as described above.
- adwn 6y ago
- gnulinux 6y agoEh, syntactically it hides the state as in `f x` has the same value every time, but calling functional programming stateless is a stretch, I consider it a half-truth told the newcomers to understand the concept. Functional languages still have state but they're abstracted out to function call. E.g. if you have a recursive descent parser written in Haskell, your program will still have state, not every time you're in `expr` function you'll have the same conceptual state. I think lack of mutation is a lot more pronounced, since it has very important implications to runtime (necessary O(logn) slow down) and requires a more sophisticated type system (linear type system) in order to emulate safe mutation.
- stutonk 6y agoIn addition, every closure is also a 'state container,' as it were. This is how some functional languages e.g. Erlang emulate mutation. A function holds the desired state in a closure and evaluates to a new function closure with updated state. It's also how a stateful object system can be trivially implemented in Scheme. A few of pieces of the Erlang OTP infrastructure are actually just an elaborate mechanism to reintroduce global mutable state i.e. the process registry or Mnesia.
- toast0 6y agoYou can use closures to store state in Erlang, but generally state is kept by passing it through to further function calls. I would say this makes Erlang mostly have explicit state, rather than saying Erlang is stateless. Ets is implemented in C as global mutable state, but you could implement it in Erlang as a process per table (and a process to hold the list of tables) with no loss of functionality. You would send messages to fetch data or update data, etc. It's a performance and memory efficiency optimization to do it with C, of course.
- kazagistar 6y agoPure functional languages, aka Haskell or Idris or whatever, don't have this property. A closure closes over values, and thus is a value, no state mutation involved. Of course, your point about complex mechanisms to reintroduce mutation into specific contexts stands (usually via monads) but those are a very different Kind of object then the functions.
- mannykannot 6y agoBefore reading this article and its follow-on, I would have completely endorsed the view that global mutable state is the worst, but for the sorts of issues the author is writing about, it seems that global mutable state is precisely the thing that makes procedural solutions more straightforward. In this case, it seems to be an intuitive and straightforward model for how both the programmer and player view the problem.
- Deadron 6y agoUntil you attempt to multithread the logic and everything breaks.
- acdha 6y agoThis is true to the extent that it’s true to say that functional programming requires you to make copies of immutable structures every time something changes, which is to say not at all. Multithreading is a problem when you have chaotic ownership of writes and updates. There are decades of prior art showing that this is a manageable problem using various strategies, and it’s not helpful to talk in absolutes rather than discussing the relative trade offs.
- deleted 6y ago[deleted]
- mannykannot 6y agoWhen your problem domain has changing global state, do you not have synchronization issues - ensuring that stale data is not used, for example - regardless of how you program it? I recall an article making this point, possibly by the same author.
- lacker 6y agoThe only thing worse than global mutable state is a complicated and buggy structure that does the exact same thing that global mutable state does
- 6y ago
- pyrale 6y ago> there is no other first-class language feature common to all the self-described "functional" languages. This line of speech usually serves to corner FP in a self-contradicting definition that can be wrestled with, and it is really aggravating. Functional programming as a concept is well defined, and it's not merely "first-order functions". The reason why many functional languages don't have the same feature set is that programming languages are works of engineering, and that the essence of engineering is to make tradeoffs. For instance, F#, a functional language, has many non-functional bits, because the goal pursued was to bring FP to .net, and they made sensible choice to promote interop above functional purity. Likewise, most people doing FP with Lisp in the early days still used mutable variables, because they were limited by the machines they had. Another example is the ability to perform all kinds of side-effects in many FP languages (Erlang, Clojure, etc.). That doesn't mean FP practicioners are totally fine with having side-effects. It means that totally separating side-effects from code in a general-purpose programming language is not always easy, and that in the interest of having useful projects, sometimes this goal is set aside. On the other hand, programs running on more limited runtimes (eg. Elm in browsers) achieve it completely. It is really a pity that so many people can't or don't want to acknowledge the nature of engineering work, and hold imperfections against the creators, or the concepts they try to implement.
- dgb23 6y agoI agree. Also in a practical sense, functional paradigms are sometimes or often implemented with a single global mutable variable as a sort of database. This pattern is quite typical for both Clojure programs and JavaScript frontend programs. It is really more about controlling state and avoiding it where deemed unnecessary, rather than adherence to a total philosophy. This is done for practical reasons (avoiding pain mostly), not for ideological ones.
- kibwen 6y ago> Functional programming as a concept is well defined, and it's not merely "first-order functions". Then, sincerely, what is the definition? Does that definition succeed in excluding languages that are not considered functional, e.g. C++, Java, Python, Go? And if it doesn't, and the definition is so broad as to encompass every language, then what is the useful purpose of going out of one's way to label a language as "functional"?
- nerdtime 6y agoThe other phenomenon here is that an imperative program with immutable state is isomorphic (exactly the same) as a functional program. Literally the same thing.
- chriswarbo 6y ago"Functional programming" is a useful description for techniques/approaches. The unifying principle tends to be (a) semantics based on definition and substitution of values/expressions (e.g. no ambient state/context) and (b) 'direct' evaluation strategy for expressions (e.g. no search, like in logic programming). "Functional language" is less concrete; it's just an informal way to imply how easy or awkward it is to use these techniques in a particular language (e.g. bash is "less functional" than ML).