Personal tools

Scrap your boilerplate

From HaskellWiki

Revision as of 21:23, 10 May 2007 by Greenrd (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Scrap Your Boilerplate is an infrastructure for generic programming in Haskell. (This should not be confused with the concept of "generic types" in Java, which in the Java context means parameterized types which allow you to specify the type of list elements and such things - a feature Haskell has had since its inception.)

In the current GHC implementation, it consists of a set of modules (Data.Generics and its submodules) and deriving support for the Data and Typeable classes from those modules.

1 Code snippets

1.1 ListifyWholeLists

This code snippet implements a variation on the listify function. listify recurses into every "node" in a datastructure, which is not so good if what you're searching for is lists (e.g. strings) and you don't want to pick up sublists (substrings, in the case of strings). This function does not recurse into lists of the type being searched for, avoiding that problem.

It illustrates the use of ShowS-style function composition as an alternative to recursive list concatenation - the latter is inefficient. It also illustrates how laziness automatically means that sublists of type [b] won't be examined, without any special effort required. Isn't laziness wonderful?

import Data.Generics
 
-- A version of Data.Generics.listify which doesn't recurse into sublists of type [b]
listifyWholeLists :: Typeable b => ([b] -> Bool) -> GenericQ [[b]]
listifyWholeLists blp = flip (synthesize id (.) (mkQ id (\bl _ -> if blp bl then (bl:) else id))) []