[Haskell-beginners] homogeneous value list

Brent Yorgey byorgey at seas.upenn.edu
Fri Mar 12 09:40:36 EST 2010


On Fri, Mar 12, 2010 at 03:31:54PM +0100, Daniel Fischer wrote:
> Am Freitag 12 März 2010 15:25:48 schrieb Salil Wadnerkar:
> > On Fri, Mar 12, 2010 at 10:21 PM, Ozgur Akgun <ozgurakgun at gmail.com> 
> wrote:
> > > Another boring variant from me then.
> > >
> > > isHomogeneous xs = all (first==) xs
> > >     where first = head xs
> >
> > Shouldn't
> > head xs
> > give an exception on an empty list?
> 
> Only if it's demanded, which it isn't here, since all doesn't look at the 
> condition when facing an empty list.
> 
> But it's good practice to never call head on a list which may be empty, so 
> I wouldn't like Ozgur's variant in real code. It's great for golf, though 
> :D

I would argue that it's good practice simply to never call head at
all.  I stopped a while ago, and I haven't missed it. =)

-Brent


More information about the Beginners mailing list