[Haskell-cafe] Re: [Haskell] View patterns in GHC: Request for feedback

Benjamin Franksen benjamin.franksen at bessy.de
Wed Jul 25 08:12:29 EDT 2007


apfelmus wrote:
> Jules Bean wrote:
>> Have you tried using pattern guards for views?
>> 
>> f s | y :< ys <- viewl s = ....
>>     | EmptyL  <- viewl s = ....
> 
> Hm, I'd simply use a plain old case-expression here
> 
>   f s = case viewl s of
>      y :< ys -> ...
>      EmptyL  -> ...
> 
> In other words, case-expressions are as powerful as any view pattern may
> be in the single-parameter + no-nesting case.
> 
> A better example is probably  zip  for sequences (Data.Sequence.Seq):
> 
>   zip :: Seq a -> Seq b -> Seq (a,b)
>   zip xs ys = case viewl xs of
>      x :< xt -> case viewl ys of
>          y :< yt -> (x,y) <| zip xt yt
>          EmptyL  -> empty
>      EmptyL  -> empty

This is how I do it, no pattern guards, no view patterns:

zip :: Seq a -> Seq b -> Seq (a,b)
zip xs ys = case (viewl xs,viewl ys) of
  (EmptyL,  _      ) -> empty
  (_,       EmptyL ) -> empty
  (x :< xt, y :< yt) -> (x,y) <| zip xt yt

This is IMHO a lot clearer than any of the alternatives you listed, except
your 'dream' (which is exactly what 'real' views would give us).

Cheers
Ben, member of the we-want-real-views-or-nothing-at-all movement ;-)



More information about the Haskell-Cafe mailing list