[Haskell-cafe] Embedded Functions in Algebraic Data Types?

Sebastian Sylvan sebastian.sylvan at gmail.com
Sun Feb 10 08:42:50 EST 2008


On Feb 10, 2008 1:34 PM, Michael Feathers <mfeathers at mindspring.com> wrote:

> 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
key')

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).

-- 
Sebastian Sylvan
+44(0)7857-300802
UIN: 44640862
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080210/66318ab6/attachment.htm


More information about the Haskell-Cafe mailing list