[Haskell-beginners] Re: homogeneous value list

Heinrich Apfelmus apfelmus at quantentunnel.de
Fri Mar 12 12:00:10 EST 2010


Thomas Davie wrote:
> Salil Wadnerkar wrote:
>> Ozgur Akgun 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?
> 
> An error, and only if it's evaluated.  Lazy evaluation means it's not evaluated here.
> 
> Of course another non-strict algorithm *might* evaluate head xs here,
> so this version won't work in all possible Haskell implementations,
> only the current ones which use lazy evaluation.

Nope, this works for any non-strict implementation. It can be deduced
from the Haskell98 language standard that  all  has the property

    all ⊥ [] = True


(Non-strict semantics doesn't tell you anything about time and space
behavior, though; that's where you need the information that the
compiler is using lazy evaluation.)


Regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com



More information about the Beginners mailing list