[Haskell-cafe] How to hold common data

Keith Wansbrough Keith.Wansbrough at cl.cam.ac.uk
Thu May 13 11:04:07 EDT 2004

> Mmm, yes I had thought of that. But I wasn't sure how it would work, as 
> you can't have variables in the same sense as imperative languages. So 
> if I create a mainloop sort of function, that  passes the old state into 
> itself, then would I have to modify the output within that fucntion to 
> get it to display to the user, correct? Thanks for the response!

Here's an example:

let f n =
  do s <- getLine
       print n
       print s
       f (n+1)
let main =
  f 1

Here the line number is the state.  You can have as many state
"variables" as you like - just add extra parameters to f.  If you have
a lot, it's probably worth using a record instead of a whole bunch of
separate variables.

--KW 8-)

More information about the Haskell-Cafe mailing list