On Feb 10, 2008 1:42 PM, Sebastian Sylvan <
sebastian.sylvan@gmail.com> wrote:
On Feb 10, 2008 1:34 PM, Michael Feathers <
mfeathers@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).
Perhaps I should add an empty MyMap too:
empty = M (\_-> Nothing )
--