* small
* useful
* demonstrate Haskell's power
* preferably something that might be a bit
tricky in another language
Something I like: A finite (binary) relation
> data Rel q = Rel { elems :: [(q,q)] }
We do not export constructors, hence
> rel xs = Rel { elems = xs }
Now, the function mirror
> mirror r = rel [ (p,q) | (q,p) <- elems r ]
is an involution, that is mirror . mirror == id. Okay, how to exploit
that? Nothing easier than that in Haskell: Just make mirror a field of Rel
> data Rel q = Rel { elems :: [(q,q)], mirror :: Rel q }
and change the construction function to
> rel xs = r
> where r = Rel
> { elems = xs
> , mirror = let m = rel [ (p,q) | (q,p) <- elems r ]
> in m { mirror = r }
> }
Note that the signatures of mirror and rel are the same as before. Try
this in your favorite language!
Regards,
--
-- Mirko Rahn -- Tel +49-721 608 7504 --
--- http://liinwww.ira.uka.de/~rahn/ ---
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe