Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  homogeneous value list (Thomas Davie)
   2. Re:  homogeneous value list (Ozgur Akgun)
   3. Re:  homogeneous value list (Daniel Fischer)
   4. Re:  homogeneous value list (Ozgur Akgun)
   5. Re:  homogeneous value list (Thomas Davie)
   6. Re:  homogeneous value list (Brent Yorgey)


----------------------------------------------------------------------

Message: 1
Date: Fri, 12 Mar 2010 14:28:29 +0000
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] homogeneous value list
To: Salil Wadnerkar <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"


On 12 Mar 2010, at 14:25, Salil Wadnerkar wrote:

> On Fri, Mar 12, 2010 at 10:21 PM, Ozgur Akgun <[email protected]> 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.

Bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100312/485afe6b/attachment-0001.html

------------------------------

Message: 2
Date: Fri, 12 Mar 2010 14:29:13 +0000
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] homogeneous value list
To: Salil Wadnerkar <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Should it? I tried to explain it, but if you are not convinced just give it
a try.


>
> Guess what? You do not need the special case for [], because "all _ [] =
> True" allways. So if the list is empty, it won't even try evaluating
> "first".
>

On 12 March 2010 14:25, Salil Wadnerkar <[email protected]> wrote:

> On Fri, Mar 12, 2010 at 10:21 PM, Ozgur Akgun <[email protected]>
> 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?
>
>
>
> >
> > Guess what? You do not need the special case for [], because "all _ [] =
> > True" allways. So if the list is empty, it won't even try evaluating
> > "first".
> >
> > Cheers,
> >
> > On 12 March 2010 14:08, Daniel Fischer <[email protected]> wrote:
> >>
> >> Am Freitag 12 März 2010 14:41:11 schrieb Daniel Fischer:
> >> >
> >> > isHomogeneous :: Eq a => [a] -> Bool
> >> > isHomogeneous [] = True
> >> > isHomogeneous (x:xs) = all (== x) xs
> >> >
> >>
> >> Variant, getting rid of the special case for an empty list:
> >>
> >> isHomogeneous :: Eq a => [a] -> Bool
> >> isHomogeneous xs = and (zipWith (==) xs (drop 1 xs))
> >> _______________________________________________
> >> Beginners mailing list
> >> [email protected]
> >> http://www.haskell.org/mailman/listinfo/beginners
> >
> >
> >
> > --
> > Ozgur Akgun
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> >
>



-- 
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100312/3cb52c2d/attachment-0001.html

------------------------------

Message: 3
Date: Fri, 12 Mar 2010 15:31:54 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] homogeneous value list
To: Salil Wadnerkar <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Freitag 12 März 2010 15:25:48 schrieb Salil Wadnerkar:
> On Fri, Mar 12, 2010 at 10:21 PM, Ozgur Akgun <[email protected]> 
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

>
> > Guess what? You do not need the special case for [], because "all _ []
> > = True" allways. So if the list is empty, it won't even try evaluating
> > "first".
> >
> > Cheers,
> >
> > On 12 March 2010 14:08, Daniel Fischer <[email protected]> 
wrote:
> >> Am Freitag 12 März 2010 14:41:11 schrieb Daniel Fischer:
> >> > isHomogeneous :: Eq a => [a] -> Bool
> >> > isHomogeneous [] = True
> >> > isHomogeneous (x:xs) = all (== x) xs
> >>
> >> Variant, getting rid of the special case for an empty list:
> >>
> >> isHomogeneous :: Eq a => [a] -> Bool
> >> isHomogeneous xs = and (zipWith (==) xs (drop 1 xs))



------------------------------

Message: 4
Date: Fri, 12 Mar 2010 14:33:49 +0000
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] homogeneous value list
To: Thomas Davie <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

On 12 March 2010 14:28, Thomas Davie <[email protected]> wrote:

>
> On 12 Mar 2010, at 14:25, Salil Wadnerkar wrote:
>
> On Fri, Mar 12, 2010 at 10:21 PM, Ozgur Akgun <[email protected]>
> 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.
>
> Bob
>


So we need to make sure our code would work in a strict environment, even
though we are using a lazily evaluated language, just in case if someone
comes up with a not-that-lazy implementation of haskell?

If I was thinking this way, I would just stop using haskell at all.


-- 
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100312/08efb5a7/attachment-0001.html

------------------------------

Message: 5
Date: Fri, 12 Mar 2010 14:39:18 +0000
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] homogeneous value list
To: Ozgur Akgun <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"


On 12 Mar 2010, at 14:33, Ozgur Akgun wrote:

> 
> 
> On 12 March 2010 14:28, Thomas Davie <[email protected]> wrote:
> 
> On 12 Mar 2010, at 14:25, Salil Wadnerkar wrote:
> 
>> On Fri, Mar 12, 2010 at 10:21 PM, Ozgur Akgun <[email protected]> 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.
> 
> Bob
> 
> 
> So we need to make sure our code would work in a strict environment, even 
> though we are using a lazily evaluated language, just in case if someone 
> comes up with a not-that-lazy implementation of haskell?
> 
> If I was thinking this way, I would just stop using haskell at all.

Not quite, you need to make sure your code works in *all* non-strict evaluation 
models.  Lazy evaluation is not the only non-strict evaluation method, another 
implementation of non-strict semantics might actually evaluate head xs.

Bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100312/6fd97c5a/attachment-0001.html

------------------------------

Message: 6
Date: Fri, 12 Mar 2010 09:40:36 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] homogeneous value list
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1

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 <[email protected]> 
> 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


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 21, Issue 11
*****************************************

Reply via email to