Wed Dec 29 1999, [EMAIL PROTECTED] ->
> Is it possible to define an inverse function such that given the functions
> f,g, and h and their inverses f',g', and h', the inverse function would
> return the appropriate inverse?
> e.g.
> inverse(f)=f'
> inverse(g)=g'
> inverse(h)=h'
Do you want to have a function
inverse :: (a -> b) -> (b -> a)
so that (inverse f) . f = id
?
that is not possible in the general case. Not to forget that even if it
was, not all functions has inverses.
However, if you restrict yourself to bounded types (types with a finite
number of elements), this is indeed possible.
inverse :: (Enum a, Bounded a, Eq b) => (a -> b) -> b -> a
inverse f x = head [y | y <- [minBound..maxBound], f y == x]
Main> inverse toLower 'a'
'A'
Main> inverse ord 90
'Z'
But it takes some time when the type is big...
n.
--
[ http://www.dtek.chalmers.se/~d95mback/ ] [ PGP: 0x453504F1 ] [ UIN: 4439498 ]
Opinions expressed above are mine, and not those of my future employees.
Skingra er! Det finns ingenting att f�rst�!
PGP signature