
Hi. I know, this is a very basic question but I've had some trouble with it. I've a list and I want to write a function isHomogeneous :: [String] -> Bool returning True is all the elements of the list are equals and False otherwise. In my first ugly test I've used a list-comprehension method, but I'm sure that exists a more elegant/short way to obtain the same result. Thanks in advance for any answer. Luca _________________________________________________________________ Tell us your greatest, weirdest and funniest Hotmail stories http://clk.atdmt.com/UKM/go/195013117/direct/01/

Am Freitag 12 März 2010 14:27:21 schrieb Luca Ciciriello:
Hi.
I know, this is a very basic question but I've had some trouble with it.
I've a list and I want to write a function
isHomogeneous :: [String] -> Bool
returning True is all the elements of the list are equals and False otherwise. In my first ugly test I've used a list-comprehension method, but I'm sure that exists a more elegant/short way to obtain the same result.
Thanks in advance for any answer.
isHomogeneous :: Eq a => [a] -> Bool isHomogeneous [] = True isHomogeneous (x:xs) = all (== x) xs
Luca

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))

Another boring variant from me then.
isHomogeneous xs = all (first==) xs
where first = head xs
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
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 Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Ozgur Akgun

On Fri, Mar 12, 2010 at 10:21 PM, Ozgur Akgun
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
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 Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Ozgur Akgun
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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

On 12 March 2010 14:28, Thomas Davie
On 12 Mar 2010, at 14:25, Salil Wadnerkar wrote:
On Fri, Mar 12, 2010 at 10:21 PM, 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.
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

On 12 Mar 2010, at 14:33, Ozgur Akgun wrote:
On 12 March 2010 14:28, Thomas Davie
wrote: On 12 Mar 2010, at 14:25, Salil Wadnerkar wrote:
On Fri, Mar 12, 2010 at 10:21 PM, 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.
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

Does this have any implications say, for using infinite data
structures? When we can we assume laziness (in order to use
abstractions like infinity), and when can we not?
On Fri, Mar 12, 2010 at 9:39 AM, Thomas Davie
On 12 Mar 2010, at 14:33, Ozgur Akgun wrote:
On 12 March 2010 14:28, Thomas Davie
wrote: On 12 Mar 2010, at 14:25, Salil Wadnerkar wrote:
On Fri, Mar 12, 2010 at 10:21 PM, 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. 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 _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Fri, Mar 12, 2010 at 09:44:18AM -0500, Ron Leisti wrote:
Does this have any implications say, for using infinite data structures? When we can we assume laziness (in order to use abstractions like infinity), and when can we not?
Not really. Personally, I find this thread a bit pedantic; at some point it's good to understand the finer points of the Haskell standard, but this really has almost no bearing on beginners. Use lazy, infinite data structures to your heart's content. =) -Brent
On Fri, Mar 12, 2010 at 9:39 AM, Thomas Davie
wrote: On 12 Mar 2010, at 14:33, Ozgur Akgun wrote:
On 12 March 2010 14:28, Thomas Davie
wrote: On 12 Mar 2010, at 14:25, Salil Wadnerkar wrote:
On Fri, Mar 12, 2010 at 10:21 PM, 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. 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 _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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

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
On Fri, Mar 12, 2010 at 10:21 PM, 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?
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
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 Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Ozgur Akgun
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Ozgur Akgun

Am Freitag 12 März 2010 15:25:48 schrieb Salil Wadnerkar:
On Fri, Mar 12, 2010 at 10:21 PM, 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?
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
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))

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
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

Am Freitag 12 März 2010 15:40:36 schrieb Brent Yorgey:
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
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
I would argue that in snub = map head . group . sort and the like, calling head is perfectly cromulent. But apart from that, I don't remember using head in the last years.

On Fri, Mar 12, 2010 at 05:15:24PM +0100, Daniel Fischer wrote:
Am Freitag 12 März 2010 15:40:36 schrieb Brent Yorgey:
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
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
I would argue that in
snub = map head . group . sort
and the like, calling head is perfectly cromulent. But apart from that, I don't remember using head in the last years.
Ah, true, I guess that's the one place I've used head as well. =) -Brent

Brent Yorgey wrote:
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. =)
Daniel Fischer wrote:
I would argue that in snub = map head . group . sort and the like, calling head is perfectly cromulent. But apart from that, I don't remember using head in the last years.
Ah, true, I guess that's the one place I've used head as well. =)
snub = concatMap (take 1) . group . sort Perhaps a tiny bit slower if it doesn't get optimized. But I consider using head a premature optimization. listToMaybe is another helpful tool for getting rid of head. Enjoy, Yitz

On Fri, Mar 12, 2010 at 01:27:21PM +0000, Luca Ciciriello wrote:
I've a list and I want to write a function
isHomogeneous :: [String] -> Bool
returning True is all the elements of the list are equals and False otherwise. In my first ugly test I've used a list-comprehension method, but I'm sure that exists a more elegant/short way to obtain the same result.
I'm a total beginner, so I don't know if this is the best way, but I tried: isHomogeneous :: [String] -> Bool isHomogeneous [] = True isHomogeneous [x] = True isHomogeneous (x:xs) = if x == head xs && isHomogeneous xs then True else False -- ======================== Roger Whittaker roger@disruptive.org.uk http://disruptive.org.uk ========================

This is not bad, I think Daniel's version is a bit more elegant. One point though, whenever you see "then True else False" you can just remove the conditional. if cexp then True else False is the same as just 'cexp' Your last line becomes simply: isHomogeneous (x:xs) = x == head xs && isHomogeneous xs
I'm a total beginner, so I don't know if this is the best way, but I tried:
isHomogeneous :: [String] -> Bool
isHomogeneous [] = True isHomogeneous [x] = True isHomogeneous (x:xs) = if x == head xs && isHomogeneous xs then True else False
-- The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.
participants (11)
-
allan
-
Brent Yorgey
-
Daniel Fischer
-
Heinrich Apfelmus
-
Luca Ciciriello
-
Ozgur Akgun
-
Roger Whittaker
-
Ron Leisti
-
Salil Wadnerkar
-
Thomas Davie
-
Yitzchak Gale