Haskell 分页:将参数值转换为整数时出错
我知道YesSOD有一个分页程序包,但我更喜欢一个简单的UI,所以我为我的应用程序创建了一个简单的分页逻辑。但是,我无法找到将参数值转换为整数的方法Haskell 分页:将参数值转换为整数时出错,haskell,yesod,Haskell,Yesod,我知道YesSOD有一个分页程序包,但我更喜欢一个简单的UI,所以我为我的应用程序创建了一个简单的分页逻辑。但是,我无法找到将参数值转换为整数的方法 import Data.Text (unpack, singleton) import Data.Maybe one = singleton '1' -- convert char to Text, required by fromMaybe getTestPanelR :: Handler Html getTestPanelR = do
import Data.Text (unpack, singleton)
import Data.Maybe
one = singleton '1' -- convert char to Text, required by fromMaybe
getTestPanelR :: Handler Html
getTestPanelR = do
ptext <- lookupGetParam "p" -- guessing returns Maybe Text
p <- fromMaybe one ptext -- ??? does not work
-- pn <- ??? Once p is extracted successfully, how to convert to an integer?
s <- runDB $ selectList [] [Asc PersonName, LimitTo 10 , OffsetBy $ (pn - 1) * 10]
(widget, enctype) <- generateFormPost $ entryForm Nothing
defaultLayout $ do
$(widgetFile "person")
更新:
我厌倦了@Ankur的建议pageNumber>=返回。(读::String->Int)。从“1”)
中,我得到以下错误:
Couldn't match expected type `String' with actual type `Text'
Expected type: Maybe Text -> String
Actual type: Maybe Text -> Text
In the return type of a call of `fromMaybe'
In the second argument of `(.)', namely `fromMaybe "1"'
Build failure, pausing...
如果将“1”更改为1(Data.Text.singleton“1”
),我仍然会收到完全相同的错误消息
谢谢
lookupGetParam
返回ParamValue
,即类型ParamValue=String
。所以基本上它是字符串而不是文本
试试这个:
pageNumber <- (lookupGetParam "p" >>= return . (read :: String -> Int) . fromMaybe "1")
谢谢@Ankur。请查看上面我的更新--我仍然得到一个错误。事实上,我之前在fromMaybe中意外使用“1”时收到了此错误消息。那时我开始阅读文本,但由于缺乏Haskell知识,我很难进行调试。你有没有想过你的建议会出什么问题?
Couldn't match expected type `String' with actual type `Text'
Expected type: Maybe Text -> String
Actual type: Maybe Text -> Text
In the return type of a call of `fromMaybe'
In the second argument of `(.)', namely `fromMaybe "1"'
Build failure, pausing...
pageNumber <- (lookupGetParam "p" >>= return . (read :: String -> Int) . fromMaybe "1")
pageNumber <- (lookupGetParam "p" >>= return . (read :: String -> Int) . unpack . fromMaybe "1")