[Haskell-cafe] Embedded Functions in Algebraic Data Types?
sebastian.sylvan at gmail.com
Sun Feb 10 08:44:29 EST 2008
On Feb 10, 2008 1:42 PM, Sebastian Sylvan <sebastian.sylvan at gmail.com>
> On Feb 10, 2008 1:34 PM, Michael Feathers <mfeathers at mindspring.com>
> > On a lark, I loaded this into Hugs this morning, and it didn't complain:
> > data Thing = Thing (Integer -> Integer)
> > But, I've never seen that sort of construct in an example. Do people
> > ever embed functions in ADTs?
> Yes, this is quite common. Here's a quick example (untested):
> data MyMap key value = M (key -> Maybe value)
> lookup (M m) key = m key
> insert (M m) key value = M (\key' -> if key' == key then Just value else m
> This is a naive data structure for storing a map as a function from the
> key to the value. Not very efficient, perhaps, but you use similar concepts
> in more useful scenarios (e.g. the State monad).
Perhaps I should add an empty MyMap too:
empty = M (\_-> Nothing )
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe