Personal tools

Global variables

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(Deleting link to irrelevant haskell cafe post. This is *not* an example of the use of the "unsafePerformIO hack" under discussion.)
(update John Hughe's functional pearl Globals.ps URL)
 
Line 30: Line 30:
 
* People have been known to use [[Top level mutable state]], otherwise known as the "unsafePerformIO hack".
 
* People have been known to use [[Top level mutable state]], otherwise known as the "unsafePerformIO hack".
   
* John Hughes has a Functional Pearl which advocates the use of [[implicit parameters]] for global variables. He also mentions the unsafePerformIO hack and the use of a Reader monad transformer, none of which are totally satisfactory: [http://www.cs.chalmers.se/~rjmh/Globals.ps Global Variables in Haskell] (2004)
+
* John Hughes has a Functional Pearl which advocates the use of [[implicit parameters]] for global variables. He also mentions the unsafePerformIO hack and the use of a Reader monad transformer, none of which are totally satisfactory: [http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.23.145&rep=rep1&type=ps Global Variables in Haskell] (2004)
   
   

Latest revision as of 01:25, 17 September 2010

Contents

[edit] 1 Question

Is there a simple and straightforward way to use global variables in Haskell?

[edit] 2 Answer

(Perhaps annoyingly) the answer to this question, like so many other frequently asked questions, is a question. "What are you trying to do?"

The reason for this is that haskell's abstractions are different from those of the mainstream imperative languages, and the mapping isn't 1-1. So for a particular 'C' feature, there may be 3 or 4 haskell features which achieve similar effects, and the correct choice depends on the detailed context.

So, in particular, all those tasks which you use global variables for in C, can be achieved in haskell, but which solution depends what you are trying to do:

  1. If you have some globals which are constant, then just define them at the top level. No problem.
     pi = 3.14
     progname = "MyCoolApp"
  2. If you have a global environment, which various functions read from (and you might, for example, initialise from a configuration file) then you should thread that as a parameter to your functions (after having, very likely, set it up in your 'main' action). If the explicit parameter passing annoys you, then you can 'hide' it with a Monad.
  3. If you have a need to store some global state which various of your functions modify, then you have a design issue. This style of design is frowned upon in C as well! The correct solution in C is to pass a 'bundle' of appropriate parameters (you might call it an environment) to those functions which need it. In C++, perl or python you'd very likely make this bundle an object. In haskell this bundle will be a data value in some custom type; and using Monads you can 'hide the details' of passing it around, if you wish.

[edit] 3 Source

[edit] 4 Meanwhile, back on Earth

  • John Hughes has a Functional Pearl which advocates the use of implicit parameters for global variables. He also mentions the unsafePerformIO hack and the use of a Reader monad transformer, none of which are totally satisfactory: Global Variables in Haskell (2004)