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

Benjamin Franksen benjamin.franksen at bessy.de
Wed Jul 25 13:28:31 EDT 2007


Dan Licata wrote:
> On Jul25, apfelmus wrote:
>> The point is to be
>> able to define both  zip  and  pairs  with one and the same operator :< .
> 
> There's actually a quite simple way of doing this.  You make the view
> type polymorphic, but not in the way you did:
> 
>    type Queue elt
>    empty :: Queue elt
>    cons :: elt -> Queue elt -> Queue elt
> 
>    data ViewL elt rec = EmptyL
>                     | elt :< rec
> 
>    view :: Queue elt -> ViewL elt (Queue elt)
>    view2 :: Queue elt -> ViewL elt (ViewL elt (Queue elt))

This is cool! The more so because 'view2' can quite easily be defined in
terms of 'view'

view2 q = case view q of
  EmptyL -> EmptyL
  x :< q' -> x :< view q'

so it suffices to provide the one-level 'view' as a library function. Does
this scale to views containing multiple nestable constructors?

It would, of course, be nice to get rid of having to write view2
altogether...

Cheers
Ben



More information about the Haskell-Cafe mailing list