4 ms·
For me, purity, functional composition, and avoiding mutable state.
by codygman 12y ago
For me, purity, functional composition, and avoiding mutable state.
- galaxyLogic 12y agoMakes sense, a function must always return the same value for the same argument. Therefore if there were mutable state you might have things that look like functions but are really not. But what is "purity"? Does it mean that you can NOT HAVE mutable state in any program written in a "purely functional" language? I still don't quite get it how a language could be "purely" functional if it has to do IO. Reading the user's input will return a different value most of the time. And what do you do in Haskel if you need to know the current TIME for any reason? It can't be returned by a "function" because its result will always be different. So there must be something ELSE besides functions in the language. But doesn't that then mean that it is no longer a "purely functional" lanaguage?
- mhogomchungu 12y agoA "pure" functional programming language has the problem you have identified and that is why haskel for a very long time was considered an academic language since it couldnt do anything useful as anything useful requires side effects like IO. The above is how haskel was introduced when i took it in CS at Syracuse University. The practicalities of haskel as a useful and practical language are realized through monads[1]. Use of monads to deal with side effects and impure world automatically make haskel "none pure". With the existence of monads,the purity of the language is a matter of opinion or degrees since it largely based on "idiomatic" usage. To say haskel is an impure language because of monads will be akin to say C# is an unsafe language because it allows pointer managements through the usage of "unsafe" keyword. [1] http://en.wikipedia.org/wiki/Monad_%28functional_programming%29 http://en.wikipedia.org/wiki/Monad_%28functional_programming...
- galaxyLogic 12y agoGood explanation, thanks. I would take from it that "purity" is relative, just as "safeness" is. There are no safe languages, nor pure ones, just some that are more pure than others. I guess I would have grasped your point earlier if Haskell had a keyword "IMPURE", just like C# has the keyword "unsafe". Come to think of it, doesn't "pure" in the context of PLs mean much the same as "safe"?