I thought I was beginning to understand IO in Haskell until I ran into the following problem.
I have the following function, which returns type IO Float:
getFundPrice :: Int -> Int -> IO Float
getFundPrice fund date = do
priceList <- getFundPrice' fund date
let h = head priceList
return h
The function getFundPrice’ uses the takusen database library and returns a list of type IO [Float].
I’m able to successfully test the getFundPrice function with Hunit using the following:
p <- getFundPrice 120 20100303
assertEqual
"get fund price"
10.286
(p)
The problem that is stumping me is the following function definition:
lastPos :: Int -> (Shares,Float) -> Period -> Fund -> (Shares,Float)
lastPos endDate begPos [] fund = (fst begPos, trnPrice)
where trnPrice = do
price <- getFundPrice fund endDate
return price
The error I’m getting when attempting to compile is ” Couldn’t match expected type Float' against inferred typeIO Float'”
I thought the *price <- getFundPrice * action would retrieve the price for me as it does with my HUnit code.
What’s different about using this in the where clause?
If you remove the explicit type signature you will see the correct type of:
Notice the
IO Floatat the end. You have definedtrnPriceto be an IO action that retrieves a floating point value. You have not executed that action to retrieve the value! You can not execute that action from anywhere except for the IO monad, whichlastPosis not in. What you can do is:Which lifts all of
lastPosinto IO.