[Haskell-cafe] How do you rewrite your code?

Henning Thielemann schlepptop at henning-thielemann.de
Wed Mar 3 18:41:13 EST 2010


Stephen Tetley schrieb:
> On 2 March 2010 19:20, Sean Leather <leather at 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.
>   
Me too.
http://haskell.org/haskellwiki/Top-level_vs._local_recursion

I have written some articles in Category:Style on that topic.



More information about the Haskell-Cafe mailing list