[Haskell-beginners] How to extend fuinctions?

Brent Yorgey byorgey at seas.upenn.edu
Fri Feb 22 20:06:36 CET 2013


On Fri, Feb 22, 2013 at 06:35:19PM +0100, Heinrich Ody wrote:
> Hi,
> 
> I have a function Delta and two sets S and A. Delta maps some elements
> from (S x A) to S.
> How can I extend Delta to some Delta' such that it assigns to every
> pair in (S x A) that Delta is not defined for a value not occuring in
> S?

If your function delta is partial, then you should model this in
Haskell by having it return a Maybe, i.e.

  delta :: S -> Maybe A

Then you can define delta' like this:

delta' s a = case delta s a of
               Nothing -> n
               Just x  -> x

If your function delta really is partial in Haskell (i.e. it sometimes
crashes or returns 'undefined') then you cannot implement delta',
because there is no way to check whether a function returns undefined.
It is not a good idea to have partial functions in Haskell anyway.

-Brent



More information about the Beginners mailing list