Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

java

Name: Anonymous 2007-04-02 21:47 ID:lENHJj++

i have to create a loop, in the loop i have to take the input values (from keyboard) and then output min/max and average of the values entered. I have no problem with the average, but the only way i can find how to figure the min/max is through a text file example, which doesnt help me when it has to be through keyboard. help pls
8)

Name: Anonymous 2007-04-03 16:30 ID:jWCrT+sL

>>13,14

EVEN BETTER:

import Control.Monad.State

data Ex = Ex Float Float [Float]

instance Show Ex where
    show (Ex min max list) =
        "minimum is: " ++ show min ++ "\n" ++
        "maximum is: " ++ show max ++ "\n" ++
        "average is: " ++ show avg ++ "\n"
      where avg = foldr1 (+) list /
                    fromIntegral (length list)

process :: StateT Ex IO ()
process = do
    lift $ putStr "Enter another number: "
    str <- lift getLine
    when (str /= "q") (do
      (Ex a b xs) <- get
      let num = read str
      let min' = if num < a then num else a
      let max' = if num > b then num else b
      let new' = Ex min' max' (num:xs)
      lift $ putStr $ show new'
      put new'
      process)

main :: IO ()
main = do
    putStr "Enter a number: "
    n <- liftM read getLine
    evalStateT process (Ex n n [n])

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List