I have a function which creates a tuple after computation, but I would like to write it to file.
I know how to write to a file using writeFile
, but do not know how to combine the computation and monads IO together in the type signature.
This is my code.
invest :: ([Char]->Int->Int->([Char], Int) )
-> [Char]->Int->Int->([Char], Int)
invest myinvest x y = myinvest x y
myinvest :: [Char]->Int->Int->([Char], Int)
myinvest w x y
| y > 0 = (w, x + y)
| otherwise = error "Invest amount must greater than zero"
where
I have a function which computes the maximum value from a list, but I want this function to receive input from a file, and then perform the computation of maximum value.
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
Please help. Thanks.
[Edit]
- My new question is at below.
The first and second question can be solve through function composition but when i try it say type mismatch.
I have check it but i couldn't find any errors.
invest :: ( [Char]->Int->Int->([Char], Int) ) -> [Char]->Int->Int-> ([Char], Int)
invest theinvest x y = theinvest x y
theinvest :: [Char]->Int->Int-> ([Char], Int)
theinvest w x y | y > 0 = (w, x + y)
| otherwise = error "Invest amount must greater than zero"
savefile :: ([Char], Int) -> IO()
savefile (x, y) = do
let name = fst (x, y)
let temp = snd(x, y)
let amount = show temp
writeFile "C:\\Invest.txt" (name ++ " " ++ amount)
test = savefile . theinvest "asd" 1234 234
The error message is
ERROR - Type error in application
* Expression : savefile . invest theinvest "234" 234 234
Term : invest theinvest "234" 234 234
Type : ([Char],Int)
* Does not match : a -> b
Please help. My return type is ([Char],Int)
. Why it complaint as a -> b
? Thanks
I solve this using command like savefile (invest theinvest "asd" 12 12) but why the operator doesn;t works ?
My fourth question is i have something like this ["peter","1000","michell","2000","kelly","3000"] and i would like to convert to [("peter",1000),("michell", 2000),("kelly",3000)]
The reading of file content is ok but i want to filter the string and get the number only. For instance, a file that has "peter 100\nasd 200"
I would like to drop the alphabet and remain the integer here. I just want the [100, 200] to be the argument the function.
Please help.
Thanks.