from defmacro - On Haskell, Intuition And Expressive Power:
Here's an example from something I've been doing for a prototype of a web application. This piece of code goes through a parse tree of Haskell source code, locates every reference to an identifier that ends with "Widget", puts it on a list, and removes duplicates so every identifier is represented in the list only once. I did this to avoid writing boilerplate code every time I create a new servlet.
extractWidgets :: (Data a) => a -> [String] extractWidgets = nub . map (\(HsIdent a)->a) . listify isWidget where isWidget (HsIdent actionName) | "Widget" `isSuffixOf` actionName = True isWidget _ = False
It took me quite a bit of time to write this piece of code. Considering how long it took to write these five lines it may seem that I wasn't very productive. However, if we consider how much this tiny paragraph of code does, a different picture begins to emerge.
Let x be a parse-tree -- a nested list of symbols. Then in K2:
a:?,//x a@&a _sm\:"*Widget"
a:distinct raze over x a where a like "*Widget"