I apologize for the bad title, but I don't quite know how to summarize this.
I'm a Haskell beginner, making a website in Haskell (Hakyll) using templates in Blaze-HTML, which uses do notation to make HTML. Here's part of my template:
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"
Now let's say I wanted to be DRY here, so I wouldn't have all the links, just a list of URLs in some kind of list comprehension. How would I do that?
You can use
mapM_here:So here we use
mapM_ :: Monad m => (a -> m b) -> t a -> m ()to process the monadic function((link ! rel "stylesheet" ! type_ "text/css" !) . href)for every element of the listthelinks.We construct the function by using operator section, so:
is equivalent with:
But we can not directly pass an
AttributeValueasxto that function: we need to use thehrefattribute, we do so by using. href, thus thus means that:So it is a more syntactically convenient way to call the
hreffunction on the item of the list, and that result in the structure oflink ! ....For the given sample list, this produces: