次のように、Web ページのタイトルを現在の年を含む文字列に設定しようとしています。
getCurrentYear :: IO String
getCurrentYear = do
now <- getCurrentTime
let today = utctDay now
let (year, _, _) = toGregorian today
return $ show year
title :: IO Html
title = do
y <- getCurrentYear
return $ toHtml $ "Registration " ++ y
getRootR :: Handler RepHtml
getRootR = do
(widget, enctype) <- generateFormPost personForm -- not important for the problem at hand, comes from the example in the yesod book
defaultLayout $ do
setTitle title -- this is where I get the type error
[...]
これをコンパイルしようとすると、setTitle
行に次のエラーが表示されます。
Couldn't match expected type `Html' with actual type `IO Html'
In the first argument of `setTitle', namely `title'
[...]
IOモナドから現在の年を取得できません(またはsetTitle
関数をそれに持ち上げます)。私はさまざまなことを試しましたが成功しませんでした。つまり、これはおそらく私がまだ Haskell の型システムを理解していないという事実に帰着します ;-) 教えてもらえますか?