5 ms·
Functional languages aren't stateless... at least not the way this person, and many others, imagine. Of course they have state, and manipulate state, and do any
by jerf 15y ago
Functional languages aren't stateless... at least not the way this person, and many others, imagine. Of course they have state, and manipulate state, and do anything else that any other language does. Therefore, they have no conceptual problem with talking to databases any more than they do talking to anything else along a socket (or byte stream).
The real get-to-the-root-problem answer, which I don't see on StackOverflow right now, is to come to a better understanding of what functional programmers mean by statelessness. Broadly: A function ought to be a function. That is, when called with the same parameters, it should return the same result. A functional programmer says that in an imperative program, there is an implicit additional parameter passed to every function, call it World, which contains vast quantities of state, global variables, global references, the IO state of the world, the ability to open sockets, etc., and that every function implicitly returns a new World value with unbounded changes made to it. Functional programming removes this implicit passing of the World value, and replaces it with an explicit version. (Or rather, the strong, modern version of "Functional Programming", which is implicitly what was specified when talking about the importance of statelessness.) In Haskell, this is the IO datatype used as a monad.
A stateless function is one that does not examine World and depends solely on its non-World parameters. You can write them in non-functional languages, they just don't particularly provide you any assistance. Functional languages are ones that do provide you assistance, such as the State monadic value, which contrary to popular belief is in fact fully stateless when the arguments to all the relevant functions are de-sugared and examined. (It has a state-like effect as the programmer thinks about it, but is actually just syntax sugar for threading a value through a series of functions, as older-school functional languages like Erlang do.)
Functional languages conceptually allow functions to apply to the real world by threading a World value through some functions, and ensuring that World values can't be reused. (How they literally function may differ in ways you don't care about for now.) This is what the IO monad is doing in Haskell, ensuring there's no way to reuse a given World psuedo-value.
So, no, functional languages have no problems talking to the database when passed the relevant World value. (Hypothetically they could be passed a restricted value that would let them only talk to the database, rather than have full access to all IO.) You just won't be able to crack open a connection to the database and start talking to it unless you're in a context where that is permitted. You won't be allowed to do it without realizing it, declaring it, and explicitly controlling it.
(Also, as is the way with this sort of post, there's some ways to quibble with it. I was trying to keep it not Haskell specific, but that sort of makes it hard to maintain consistent terminology, especially when speaking to novices. Perhaps I should just have gone for Haskell only.)
- Roboprog 15y agoI think another name for "World" is "DATA DIVISION", at least in some circles. Or at least it would be, if more Java programmers actually recognized the language idiom that they are really programming. (says the tired Java programmer)
- Roboprog 15y agoThe entire "monad" discussion is pretty hard to get your head around. Perhaps part of the problem is that it represents not just side effects, but also forced ordering of operations (albeit, largely to allow for those effect-full interactions with the outside world). I think that maybe for "industrial" programmers, we need a language, perhaps something like Scala, where the default is pure functions and immutable data, but you can tag things (functions & data) as "mutants", er, "mutable"/"mutates", and those tags "taint" what those elements can be combined with. Call something with side effects from something that should be pure, and get an error, that sort of thing. Then, teach people to use mutable data as sparingly as possible. Maybe wrap database updates in some kind of (small!) message handling actor?
- jerf 15y ago"Call something with side effects from something that should be pure, and get an error, that sort of thing." If you enforce that, you get Haskell, where you have no choice but to wrap your head around isolating your IO layer from everything else. If you don't enforce that, you don't get very much material difference in practice. I'm concerned that in practice, 99% purity isn't very useful. We shall see. (You get advantage from 0 - 50%, then I think a long plateau, then a sharp spike right at 100%. It can be hard to make the leap, since there's no way to ease into it.) "The entire "monad" discussion is pretty hard to get your head around. Perhaps part of the problem is that it represents not just side effects, but also forced ordering of operations (albeit, largely to allow for those effect-full interactions with the outside world)." Well, part of the problem is that "monad" doesn't mean any of those things. It's an interface (very much like the Java sense of the term) which has some implementations that enforce those properties, and other implementations that do not. Which is to say that yes, it is hard to get your mind wrapped around, but this is largely because of the spray of terrible, terrible explanations of the monad interface sprayed about by people who barely understand it themselves. The best way to actually learn them is Learn You a Haskell; if you've already learned basic Haskell syntax you can leap straight to chapter 11[1]. (Do not jump straight to the Monad chapter 12. Start at 11. Start earlier if you don't know the syntax.) "Monad" is an adjective, not a noun. [1]: http://learnyouahaskell.com/functors-applicative-functors-and-monoids http://learnyouahaskell.com/functors-applicative-functors-an...