我为糟糕的头衔道歉,但我不太清楚如何总结这一点。
我是Haskell的初学者,使用Blaze-HTML中的模板在Haskell(Hakyll)创建一个网站,该模板使用符号来制作HTML。这是我模板的一部分:
defaultTemplateRaw :: Html
defaultTemplateRaw = html $ do
H.head $ do
meta ! httpEquiv "Content-Type" ! content "text/html; charset=UTF-8"
H.title "My Hakyll Blog - $title$"
link ! rel "stylesheet" ! type_ "text/css" ! href "/css/main.css"
link ! rel "stylesheet" ! type_ "text/css" ! href "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
link ! rel "stylesheet" ! type_ "text/css" ! href "https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
现在让我们说我想在这里干,所以我不会拥有所有的link
,只是列出某种列表理解的URL列表。我该怎么办?
答案 0 :(得分:4)
您可以在此处使用mapM_
:
thelinks :: [AttributeValue]
thelinks = ["list", "of", "hrefs"]
defaultTemplateRaw :: Html
defaultTemplateRaw = html $ do
H.head $ do
meta ! httpEquiv "Content-Type" ! content "text/html; charset=UTF-8"
H.title "My Hakyll Blog - $title$"
mapM_ ((link ! rel "stylesheet" ! type_ "text/css" !) . href) thelinks
所以我们在这里使用mapM_ :: Monad m => (a -> m b) -> t a -> m ()
来处理列表((link ! rel "stylesheet" ! type_ "text/css" !) . href)
的每个元素的monadic函数thelinks
。
我们使用operator section构建函数,所以:
(link ! rel "stylesheet" ! type_ "text/css" !)
等同于:
\x -> link ! rel "stylesheet" ! type_ "text/css" ! x
但是我们无法直接将AttributeValue
作为x
传递给该函数:我们需要使用href
属性,我们使用. href
这样做,因此意思是:
(link ! rel "stylesheet" ! type_ "text/css" !) . href
-----------------------------------------------------------------------
= (\x -> link ! rel "stylesheet" ! type_ "text/css" ! x) . href
-----------------------------------------------------------------------
= \y -> (\x -> link ! rel "stylesheet" ! type_ "text/css" ! x) (href y)
-----------------------------------------------------------------------
= \y -> (link ! rel "stylesheet" ! type_ "text/css" ! (href y))
因此,在列表项上调用href
函数是一种语法上更方便的方法,并且结果为link ! ...
。
对于给定的样本列表,这会产生:
Main L R> putStrLn $ L.unpack $ R.renderHtml defaultTemplateRaw
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>My Hakyll Blog - $title$</title><link rel="stylesheet" type="text/css" href="list"><link rel="stylesheet" type="text/css" href="of"><link rel="stylesheet" type="text/css" href="hrefs"></head></html>