这个问题与此有关 Haskell Input Return Tuple
我想知道如何将monad IO的输入传递给另一个函数以进行一些计算。
其实我想要的是
-- 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
在第二个示例中,从文件中读取maxinvestinput并将数据转换为期望的类型maximuminvest。 请帮忙。
感谢。
答案 0 :(得分:8)
答案 1 :(得分:3)
我不确定我理解你的问题,但我会尽我所能回答。如果我理解正确的话,我已经简化了一些事情以获得问题的“肉”。
maxInvestInput :: IO [Integer] maxInvestInput = liftM convertToIntegers (readFile "foo") maximumInvest :: Ord a => [a] -> a maximumInvest = blah blah blah main = do values <- maxInvestInput print $ maximumInvest values OR main = liftM maximumInvest maxInvestInput >>= print