Peter Stuifzand

First try Haskell web applications

I’m trying Haskell for writing Web applications. It should be really easy, but at the moment I’m missing the examples. The documentation doesn’t really help for generating the XHtml.

The following bit of code is something that I found: Haskell and Web applications. The article contains a simple example. I changed the code in the example to use a table instead of the spans. This turned out to be really easy to do.

renderData :: (Data a) => a -> Html
renderData i = table << mapFields renderField i
    where renderField (name, val) =
                tr << [ (th << renderFieldName (name ++ ":")) ! [ align "left" ]
                      , td << (read val :: String)

I added the call to read to remove the quotes around the values. The << operator adds the objects to the right as the children of the object to the left. Because of the way this is specified this can a list or a single object.

The ! operator adds an attribute to the object on the left.

There need to be more examples for Haskell that are easy to understand and change, because it is hard to understand the ways that functions work from the documentation (types and functions) alone.

I hope this will help some of you trying to write some Haskell code.

© 2023 Peter Stuifzand