On 2 March 2010 19:20, Sean Leather <leat...@cs.uu.nl> wrote:

> My question is simple:
>
>    How do you rewrite your code to improve it?
>


Hi Sean - excellent question!

Some things I do...

Quite often I do a 'worker-wrapper-lite' rewrite i.e. change a
function to perform its recursive work in a step rather than calling
the function again with all the arguments, e.g.


> para :: (a -> ([a], b) -> b) -> b -> [a] -> b
> para phi b = step
>    where step []     = b
>          step (x:xs) = phi x (xs, step xs)


rather than...

> para_ :: (a -> ([a], b) -> b) -> b -> [a] -> b
> para_ phi b []     = b
> para_ phi b (x:xs) = phi x (xs, para_ phi b xs)

I'm doing no type changing to improve efficiency so it isn't a real
worker-wrapper, but I usually find the 'step' style more pleasing,
especially when the code is somewhat more complicated than the
paramorphism above.


Another one is to eliminate do-notation, generally I do this by using
the liftM2 family more appropriately, sometimes by using my own
monadic combinators - for instance quite a few operators in
Control.Exception are useful for other monads rather than IO so I've
versions with more general types in the Utils module that add to my
projects once they get above a certain size.

Generally my types change only when I realize I hadn't got them right
in the first instance. I can't think of instances where I've
generalized types to make them functors and so could use Traversable,
Foldable... But I have had a couple instances where I've needed to
change the type of a 'leaf' in a structure so realized that the
containing structure was obviously a functor.


Best wishes

Stephen
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to