[Haskell-beginners] Re: Why doesn't ghci load this?

7stud bbxx789_05ss at yahoo.com
Sat Mar 28 02:01:19 EDT 2009


Brandon S. Allbery KF8NH <allbery <at> ece.cmu.edu> writes:

> 
> On 2009 Mar 28, at 0:59, Jeff Lasslett wrote:
> > myGroupBy :: (a -> a -> Bool) -> [a] -> [[a]]
> > myGroupBy p xs =
> > foldr step [[]] xs
> > where
> >  -- step :: a -> [ [ a ] ] -> [ [ a ] ]
> >  step x acc@( ys : yss )
> >   | null ys                      = [ x ] : []

I wonder whether that line does anything?  foldr is defined like
this:

foldr step zero (x:xs) = step x (foldr step zero xs)

So it seems to me that when the step function doesn't affect
either the accumulator(=zero) or xs, then the step function
won't have any effect on the result foldr produces. 

The return value from your 'null ys' guard does not affect
xs, nor does it change the accumulator acc.  So how does
it affect the result of foldr?











More information about the Beginners mailing list