It seems to me you are asking for this unsafePerformIO hack:
http://www.haskell.org/haskellwiki/Top_level_mutable_state
 
here your example:
 
 
import Control.Concurrent.MVar
 
func w = w*var
 
var :: Int
var = unsafePerformIO $ readMVar myGlobalMVar
 
{-# NOINLINE myGlobalMVar #-}
-- NOINLINE pragma to force this piece of code to exist exactly once in the program. inlining might cause Alzheimer's disease.
myGlobalMVar :: MVar Int
-- needs to have one explicit type (no type variables allowed) to force the value not to be read having a different type than it has been written. schizophrenic. gaaah.
myGlobalMVar = unsafePerformIO newEmptyMVar
 
main = do
  putMVar myGlobalMVar 6
  print $ func 7
 
 
 
 
 
Gesendet: Donnerstag, 27. März 2014 um 11:58 Uhr
Von: "Chapman, Anthony Sergio" <a.s.chapman.10@aberdeen.ac.uk>
An: "haskell-cafe@haskell.org" <haskell-cafe@haskell.org>
Betreff: [Haskell-cafe] Main function variables made global.
Hello, 

 

My problem is as follows (not actual code), I declare a function outside of the main function 

 

func w = w * var

 

I get the value of  'var' from a text file in the main function (as the user types the name of the file into terminal) 

 

I need a way for 'func' to see what 'var' is without  having to change it's type (ie make it into a function which take a 'w' and a 'var'. 

 

The reason I can't change the type is that I then pass the function to a PSO module which has strict declarations of a function and I would rather not change the PSO module. 

 

I also can't put 'func' into main because 'w' isn't specified. The PSO will find the best w so I don't get it a value. 

 

I have thought about making a dummy function which has an input of 'w' and 'var' and an output of a function of just 'w'. Something like: func w = func' w var.

 

I got the function for PSO to optimise and the function to get the data I want from a text file working great, they both do what they're supposed to do. I just need the function of PSO to see the data I get from the file. 

 

If you need any more information (sorry if I've been too vague) please let me know. 

 

Thank you for your time.​

 

_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe