【发布时间】:2011-07-24 23:37:27
【问题描述】:
我以为我开始了解 Haskell 中的 IO,直到遇到以下问题。
我有以下函数,它返回类型 IO Float:
getFundPrice :: Int -> Int -> IO Float
getFundPrice fund date = do
priceList <- getFundPrice' fund date
let h = head priceList
return h
函数 getFundPrice' 使用 takusen 数据库并返回一个 IO [Float] 类型的列表。
我能够使用 Hunit 成功测试 getFundPrice 函数,使用以下方法:
p <- getFundPrice 120 20100303
assertEqual
"get fund price"
10.286
(p)
困扰我的问题是以下函数定义:
lastPos :: Int -> (Shares,Float) -> Period -> Fund -> (Shares,Float)
lastPos endDate begPos [] fund = (fst begPos, trnPrice)
where trnPrice = do
price <- getFundPrice fund endDate
return price
我在尝试编译时遇到的错误是“无法匹配预期的类型Float' against inferred typeIO Float'”
我认为 *price 操作会像处理我的 HUnit 代码一样为我检索价格。
在 where 子句中使用 this 有什么不同?
【问题讨论】:
-
确保你清楚地了解
return在 Haskell 中的作用。