[Haskell-cafe] introspection | meta data

Evan LaForge quinn at burn.ofb.net
Thu Aug 5 14:58:06 EDT 2004


> Basically I need to loop over the fields in my record. I came up with 
> functions like this:
...
> setSFField :: Int -> SearchFilter -> String -> SearchFilter
> setSFField 1 (a,b,c,d,e) f = (f,b,c,d,e)
> setSFField 2 (a,b,c,d,e) f = (a,f,c,d,e)
> ....

Can you name these fields?  If so, haskell has (sorta clumsy) named records,
and you can select and update fields by name, and you can replace
'setSFField 3 sf x' with 'sf {somefield=x}'

> The only thought I had was of using lists, but the this would mean I loose 
> pattern matching against all values at once which is appealing for calrify 
> of code.

You can pattern match lists, just replace () with [] above.  You just can't
get a type error if it's the wrong length (but see the previous thread).  If
your data is of variable length and one type, then lists are what you want.
If it has names and you often only want one field, then records.  If it's just
bundling a small number of variously-typed values, and you typically need all
of them at once, then tuples.  Otherwise, there are usual data structures in
the stdlib, like tables (haskell calls them finite maps).  You could say FM is
to record as list is to tuple, sorta ;)

> So what is the general haskell approach to this type of introspection/meta 
> data problem... ?

Others have given good answers for this, but I suspect you may have chosen the
wrong data structure...

A C array of pointers maps closest to a MutableArray, which is mostly a list
with different performance.  Unless you're casting pointers, in which case
Dynamic types or the Generic stuff is maybe what you want.  Or a redesign ;)


More information about the Haskell-Cafe mailing list