3 ms·
> It is a functional language, which means that code has no side effects. This property of declarative languages in general and pure functional programming in p
by masklinn 15y ago
> It is a functional language, which means that code has no side effects. This property of declarative languages in general and pure functional programming in particular means that the program can be assumed to do exactly what it says on the tin and no more. The absence of side effects makes it possible to do a formal correctness proof of the program because the program code describes the whole computation. This assumption cannot be made with imperative languages such as C.
As much as I love Erlang, nothing in this paragraph applies to it (it would apply much, much better to Haskell though still not perfectly): Erlang is not a pure language and it does not put any limitation on side-effects (the way Haskell does by putting them in separate monadic containers unless an `unsafe` function is used).
Erlang does use immutability (its only mutable data structure is the process dictionary[-1]), it does not share state between concurrent processes[0] and features pattern-matched bindings (technically `=` in Erlang performs pattern-matching, not equality, but if a matchable part is unbound Erlang will simply fill it with the correspondence from the other side, as a result it behaves much like single assignment in many situations), which do help in making code pure and correct, but fundamentally its approach to reliability is not to make code unbreakable (the way you'd do by formally proving it for instance) but by making recovery and error management easy, simple and widely supported by the runtime and its libraries (OTP), in (no small part) part by applying the telecom-borne principles of separating concerns (between the thing that does the job and the thing which recovers the error in case the first one blows up) and building redundant systems (if you have a single machine and it crashes you're gone, if you have 2 machines and the first one crashes the second one can handle things until the first one comes back (and you can have a third machine overseeing that with its own hot spare), Erlang encourages doing that at the process level)
[-1] a process mailbox might also be considered mutable, in a way
[0] it does share memory for big binaries — they are shared and reference-counted — but these binaries are immutable so no state
- tel 15y agoI think the dialogue is starting to see functional as a positive thing but now that the distinction between "purely functional" and "has first order functions" is becoming increasingly important, the effort previously put in place to blur it is backfiring.
- masklinn 15y agoWell it is true that ("sequential") Erlang is a functional language. It was built as a functional language and has pretty much all the features usually associated with functional languages. It does not just "have first-class functions". But it is important to understand what "pure" is and is not, that almost no language is pure (because it's hard to produce useful pure software, or even to define how it would work precisely) and that very few strongly encourage purity (at the language level, many do as a social level but offer no help or support for actually doing it when writing code)
- jberryman 15y ago"functional" should never have meant "has first-class functions" to anyone.
- tel 15y agoI agree, but it was often marketed that way.
- viscanti 15y agoThat's exactly what functional means. You're conflating functional and pure. They happen to be (essentially) the same thing in Haskell, but that's not always the case. A functional language can get you really close to pure (but good luck doing anything interesting without monads or mutable state), but even the purest languages cheat. Putting your side-effect code in a main function might help you catch bugs and limit potential side-effects, but it's still not completely pure (and for good reason, at the end of the day you actually want to do stuff).
- zopa 15y ago"Pure" is an adjective applied to "functional" so I don't see the conflation as odd. A language like JavaScript has first class functions, so it's a little bit functional. But no more than that, because most expressions aren't (mathematical) functions. Haskell is purely functional because everything's a function. (I/O primitives like getChar excepted, etc., etc.) Meanwhile, languages like Haskell don't cheat quite as much as you suggest. Monads are functions, and even in the IO Monad you're doing purely functional manipulation of IO primitives. Think of it as a purely functional funnel through which the dirty data of reality is poured.