3 ms·
This seems to be missing the point. Yes, it's true that the functional programming equivalent of functions with side effects is functions with lots of extra ar
by ajross 17y ago
This seems to be missing the point. Yes, it's true that the functional programming equivalent of functions with side effects is functions with lots of extra arguments to capture the equivalent state.
Which is kind of the point: in an imperative language, you can capture all that complexity -- all 316 arguments, even -- with a few quick hacks to the side effect behavior of your functions. And that's a feature. It makes things simpler sometimes (obviously it can hurt, too -- tools can't substitute for taste). And it just can't be expressed in functional terms.
- jrockway 17y agoAnd it just can't be expressed in functional terms. Incorrect. Say you want to read some global state: a = do info <- ask return (info + 1) b = do info <- ask moreInfo <- a return (info + moreInfo) runReader b 42 -- ==> 42 + (42 + 1) ==> 85 This lets you weave information in as deeply as you want. If b doesn't need "info" but a does, and b calls a, that's still fine. You don't need to change b to make the information available to a. Just like a "global variable". You can also accumulate arbitrary information: fib :: Int -> Writer [String] Int fib 0 = tell ["fib 0"] >> return 1 fib 1 = tell ["fib 1"] >> return 1 fib x = do tell ["fib " ++ show x] a <- fib (x - 1) b <- fib (x - 2) return $ a + b runWriter (fib 4) ==> (5, ["fib 4", "fib 3", ...]) Again, very much like a global variable holding log data, but without many undesirable possibilities (like clobbering the log history accidentally, or performing IO during the computation) that are inherent in "global variables". And of course, you can read and write state, if you want: inc :: State Int Int inc = do old <- get let new = old + 1 put new return new fib 0 = inc >> return 1 fib 1 = inc >> return 1 fib x = do inc a <- fib (x - 1) b <- fib (x - 2) return $ a + b runState (fib 25) 0 ==> (121393, 242785) This is just like imperative programming, except easier and more fine-grained. Note how the number of times "fib" has been called is scoped to the "runState" invocation, and that we don't even need a "variable name". And as with Reader and Writer, if a called function needs the State, the calling function doesn't need to pass it in. So basically, many variations on the hacks that imperative programmers can be implemented easily in functional languages. Imperative programming languages give you a box to do with as you please, but functional programming languages can give you a box that you can prevent yourself from accidentally misusing. The best of both worlds.
- ajross 17y agoDoesn't the fact that you had to write this treatise (which, due only partially to syntax impedance, I frankly don't understand) to explain how to set a flipping global variable maybe clue you in that it's kinda complicated in functional languages? :)
- pictureotf 17y agoThis says more about your understanding of Haskell than anything else, really. The GP's example is not a _global_ variable, by the way. Global variables are not really available in Haskell, except if you use unsafePerformIO: import System.IO.Unsafe {-# NOINLINE global #-} global :: IORef Int global = unsafePerformIO $ newIORef 0
- ajross 17y agoIgnorant as charged, I guess. And the fact that neither of you can see the embarassing complexity of these solutions relative to, again, setting a variable reflects what, your corresponding ignorance of von neumann architectures?
- jrockway 17y agoOh, we see the "complexity" in implementing this. (Note: not very complex in real life.) But we also see the embarrassing complexity of maintaining software that uses a "quick global variable" here and there. It's nice to know what your software does without having to guess and pray. That is worth saying "runState foo 42" instead of just "foo".
- ajross 17y agoRight. So you guys are purists who refuse to use imperative constructs even when they're objectively simpler, either because (1) you see hidden costs the rest of us don't or (2) you're wrong. Isn't that the point of this whole argument?
- 17y ago