
Hello, I'm new to haskell and want to write a small program for some winder calcucations. I want to input 10-15 values and based on this input doing some calculations. After this calculations I want to print out the results. My problem is: How can I get the input values into the calculations and back the result to the output. In an other language I would use global variables for this. So what should I use in haskell? An other point is that I want to separate the input from the calculations and the output. How can I achieve this with haskell? Modules? Data types? See some pseudo code below. inputvalues :: IO() inputvalues = do hSetBuffering stdout NoBuffering putStr "Berechnung Bremsgenerator \n\n" ve <- readNum "Bitte Maschinengeschwindigkeit (m/min) angeben: " de <- readNum "Bitte Durchmesser der vollen Rolle eingeben (mm): " dewalze <- readNum "Bitte Durchmesser der Walze eingeben (mm): " dewand <- readNum "Bitte Wandstaerke der Walze eingeben (mm): " and so on .............. readNum :: String -> IO Float readNum text = do putStrLn text readLn -- calculations nvr :: Float-> Float-> Float nvr ve de = ve / de /pi *1000 pzugmin :: Float-> Float-> Float->Float pzugmin ve femin be = 1.666 * ve * femin * be * 0.00000001 pzugmax :: Float-> Float-> Float->Float pzugmax ve femax be = 1.666 * ve * femax * be * 0.00000001 and so on .............. outputvalues = do putStr "\n\nErgebnisse Bremsgenerator \n\n" putStr "Drehzahl leere Trommel: " putStr(show(round(nvr))) putStr " U/min \n" putStr "Drehzahl volle Trommel: " putStr(show(round(drehbgmax))) putStr " U/min \n" and so on ...................... Thanks Thomas

On Mar 25, 2008, at 09:13, Thomas Engel wrote:
My problem is: How can I get the input values into the calculations and back the result to the output.
In an other language I would use global variables for this. So what should I use in haskell?
(I think I know enough haskell to answer this, but I may be telling you all the wrong things.) You'll have to pass the values through, though this can be hidden to some extent. Try changing your functions to have types type Input = (Float, Float, ...) type Output = (Float, Float, ...) inputvalues :: IO Input -- add a "return (ve,de,...)" to the end compute :: Input -> Output outputvalues :: Output -> IO () You could then wrap them in main :: IO () main = do input <- inputvalues outputvalues (compute input) This should do what you want. It might be improved upon by using data types with appropriately named fields instead of tuples so as to keep your variable names around. Cheers Robert

Hi Robert
(I think I know enough haskell to answer this, but I may be telling you all the wrong things.)
Why? What's wrong?
You'll have to pass the values through, though this can be hidden to some extent. Try changing your functions to have types
type Input = (Float, Float, ...) type Output = (Float, Float, ...)
inputvalues :: IO Input -- add a "return (ve,de,...)" to the end compute :: Input -> Output
How can I combine several function (calculations) in this function?
outputvalues :: Output -> IO ()
You could then wrap them in
main :: IO () main = do input <- inputvalues outputvalues (compute input)
This should do what you want.
Thank's for your hints.
It might be improved upon by using data types with appropriately named fields instead of tuples so as to keep your variable names around.
Can you give me an example how do to it with named fields? Thanks Thomas

On Mar 25, 2008, at 13:03, Thomas Engel wrote:
type Input = (Float, Float, ...) type Output = (Float, Float, ...)
inputvalues :: IO Input -- add a "return (ve,de,...)" to the end compute :: Input -> Output
How can I combine several function (calculations) in this function?
compute (ve,de,dewalze,dewand,...) = ( nvr ve de, pzugmin ve femin, pzugmax ve femax be, ... )
Can you give me an example how do to it with named fields?
See e.g. http://www.haskell.org/tutorial/moretypes.html#sect6.2 . However, I'm not so sure anymore they'd be of much help here. Cheers Robert

Thomas Engel wrote:
inputvalues :: IO Input -- add a "return (ve,de,...)" to the end compute :: Input -> Output
How can I combine several function (calculations) in this function?
compute1 :: Input -> Output1 compute2 :: Input -> Output2 combine :: Output1 -> Output2 -> Output compute input = combine c1 c2 where c1 = compute1 input c2 = compute2 input etc Claude -- http://claudiusmaximus.goto10.org

Thomas Engel
My problem is: How can I get the input values into the calculations and back the result to the output.
In an other language I would use global variables for this.
That would be bad style.
An other point is that I want to separate the input from the calculations and the output.
So what should I use in haskell?
I think your pseudocode is right on track.
inputvalues :: IO ()
The most obvious way to do it would be to define data Input = ... data Output = ... input_values :: IO Input calculate :: Input -> Output ouput_values :: Output -> IO () then: main = do i <- input_values let o = calculate i output_values o or: main = output_values . calculate =<< input_values -k -- If I haven't seen further, it is by standing in the footprints of giants
participants (4)
-
Claude Heiland-Allen
-
Ketil Malde
-
Robert Vollmert
-
Thomas Engel