I’m looking for a way to dynamically define functions in Haskell, or for Haskell’s idiomatic equivilent of which I’m clearly not aware.
The scenario is as follows: I have a tagWithAttrs function that generates new functions based on the provided String argument. The definition looks something like this:
tagWithAttrs :: String -> ([(String, String)] -> [String] -> String)
tagWithAttrs tagName = [...] -- Implementation ommited to save room.
h1 :: [(String, String)] -> [String] -> String
h1 = tagWithAttrs "h1"
main :: IO ()
main = putStrLn $ h1 [("id", "abc"), ("class", "def")] ["A H1 Test"]
-- Should display '<h1 id="abc" class="def">A H1 Test</h1>'.
So far so good. But the line in which I assign h1 is one of many, since I’d have to do that for every single HTML tag I’m defining. In Python, I’d loop over a list of the HTML tag names, inserting each respective result from tag_with_attrs into the dictionary returned by globals(). In short, I’d be inserting new entries into the symbol table dynamically.
What is the Haskell equivilent of this idiom?
Btw, I’m fully aware that I’m duplicating the work of many existing libraries that already do HTML tags. I’m doing this for a toy project, nothing more 🙂
EDIT: Some posted solutions are suggesting mechanisms that still rely on defining the end result tag functions one-by-one. This violates DRY, else I would have just done it how I was doing it. It’s that DRY violation that I’m trying to side-step.
Haskell is statically typed, which means that all symbols must be type-checked at compile time. So that means you can’t add entries into the symbol table at runtime.
What you want is meta-programming. Where code runs at compile time to generate other code (that you naturally and rightly feel lazy to type). That implies something like a macro system .
Haskell does not have macros, but there is template Haskell:
http://www.haskell.org/haskellwiki/Template_Haskell
As with macros, the idea is that you write a function that generates an
AST. The meta-function takes the name of the function you want to use (in your
case,
div,ul,lietc) and generates the AST of a functional with that name.A bit overkill, but if you really want to do it this is a relatively simple tutorial:
http://playingwithpointers.com/archives/615