[Haskell-cafe] Why isn't pattern matching lazy by default?
Isaac Dupree
isaacdupree at charter.net
Wed Sep 19 15:52:38 EDT 2007
C.M.Brown wrote:
> Hi Miguel,
>
>> See, in let or where constructs you don't have a choice; you can't do
>> different things depending on whether some value is Just x or
>> Nothing. Therefore, there is no need to perform pattern matching
>> strictly.
>
> This is not entirely true. This is actually one of those niches in Haskell
> where the left to right is not quite the same as right to left. A let can be converted to a where
> but the other way round may require a case introduction.
>
> So just like you can define:
>
> f (Just x) = x
> f Nothing = error "Nothing"
>
> You can also define:
>
> f x = g x
> where
> g (Just x) = x
> g Nothing = error "Nothing"
>
> g is strict in its first argument. Declared in a let it would look like:
>
> f x = let g x = case x of
> (Just y) -> y
> Nothing -> error "Nothing" in g x
>
> Again, g must be strict in its first argument.
or
f x = let
g (Just x) = x
g Nothing = error "Nothing"
in g x
also works perfectly fine. The (Just x) and Nothing are not
let-patterns, they are function-definition-patterns, and of course are
strict since they make a decision.
Isaac
More information about the Haskell-Cafe
mailing list