[Haskell-cafe] CPS versus Pattern Matching performance

Tony Morris tmorris at tmorris.net
Tue Jul 10 04:47:52 EDT 2007


Thanks Don,
Is your explanation specific to maybe? Or does that apply to all functions?

Suppose the following function for lists:

f :: [a] -> b -> (a -> [a] -> b) -> b

...instead of pattern matching [] and (x:xs)

Tony Morris
http://tmorris.net/



Donald Bruce Stewart wrote:
> tmorris:
>> When you you use maybe :: b -> (a -> b) -> Maybe a -> b instead of
>> pattern matching a returned Maybe value?
>>
>> Is there something a bit more concrete on this issue?
> 
> You mean, versus using 'case' or sugar for case?
> It'll just inline to the same code.
> 
> For example:
> 
>     maybe 10 (\n -> n + 1) bigexpr
> 
>  => {inline maybe}
> 
>     (\n f x -> case x of
>         Nothing -> n
>         Just x  -> f x) 10 (\n -> n + 1) bigexpr
> 
>  => {reduce}
> 
>     case bigexpr of
>         Nothing -> 10
>         Just x  -> x+1
> 
> So use 'maybe' if its clearer -- it doesn't cost anything.
> 
> -- Don
> 
> 
> 


More information about the Haskell-Cafe mailing list