Haskell IO Passes to Another Function

Posted by peterwkc on Stack Overflow See other posts from Stack Overflow or by peterwkc
Published on 2010-06-18T07:13:52Z Indexed on 2010/06/18 7:33 UTC
Read the original article Hit count: 292

Filed under:
|
|
|
|

This question here is related to http://stackoverflow.com/questions/3066956/haskell-input-return-tuple

I wonder how we can passes the input from monad IO to another function in order to do some computation.

Actually what i want is something like

-- First Example
test = savefile investinput 
-- Second Example
maxinvest :: a
maxinvest = liftM maximuminvest maxinvestinput

maxinvestinput :: IO()
maxinvestinput = do
    str <- readFile "C:\\Invest.txt"
    let cont = words str
    let mytuple = converttuple cont
    let myint = getint mytuple

    putStrLn ""

-- Convert to Tuple
converttuple :: [String] -> [(String, Integer)]
converttuple [] = []
converttuple (x:y:z) = (x, read y):converttuple z

-- Get Integer
getint :: [(String, Integer)] -> [Integer]
getint [] = []
getint (x:xs) = snd (x) : getint xs

-- Search Maximum Invest
maximuminvest :: (Ord a) => [a] -> a
maximuminvest [] = error "Empty Invest Amount List"
maximuminvest [x] = x
maximuminvest (x:xs)   
     | x > maxTail = x  
     | otherwise = maxTail  
     where maxTail = maximuminvest xs 

In the second example, the maxinvestinput is read from file and convert the data to the type maximuminvest expected. Please help.

Thanks.

© Stack Overflow or respective owner

Related posts about function

Related posts about haskell