
Hi there, The best way I can explain what I'd like to do is show an example, so here goes: same :: a -> a -> Bool same x x = True same _ _ = False Is there any way to do this in Haskell? The actual reason I'd like to do this is something slightly more complicated. I want to match against a list of elements, like so: foo [x,x,x] = ... Thanks for any help. - Alex

Alex Watt wrote:
The best way I can explain what I'd like to do is show an example, so here goes:
same :: a -> a -> Bool same x x = True same _ _ = False
Is there any way to do this in Haskell?
No, this is not possible. Each variable bound in a pattern has to be different. Maybe guards are a good alternative: same :: Eq a => a -> a -> Bool same x y | x == y = True same _ _ = False Note that you have to add an Eq constraint.
The actual reason I'd like to do this is something slightly more complicated. I want to match against a list of elements, like so:
foo[x,x,x] = ...
Maybe you can employ the "all" function all :: (a -> Bool) -> [a] -> Bool in a guard, something like foo (x:xs) | all (x ==) xs = ... where you explicitly check that all list elements are equal to the first. Tillmann

On 2008 Aug 2, at 22:09, Alex Watt wrote:
same :: a -> a -> Bool same x x = True same _ _ = False
Is there any way to do this in Haskell? The actual reason I'd like to do this is something slightly more complicated. I want to match against a list of elements, like so:
Not with pattern matching by itself; using a name in a pattern creates a new binding. You can use guards:
same :: a -> a -> Bool same x y | x == y = True same _ _ = False
which simplifies to
same :: a -> a -> Bool same x y = x == y
foo [x,x,x] = ...
import Data.List foo :: [a] -> Bool foo x@(y:_:_:[]) = filter (== y) x
This becomes unwieldy for large lists; you would instead switch to a guard:
import Data.List foo :: [a] -> Bool foo x@(y:x') | length x == N = filter (== y) x' -- N is your expected list length foo _ = False
or, possibly saving a few microseconds
import Data.List foo :: [a] -> Bool foo (y:x) | length x == N = filter (== y) x -- n is one less than your list length foo _ = False
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Hey Alex, On 3 aug 2008, at 04:09, Alex Watt wrote:
Hi there,
The best way I can explain what I'd like to do is show an example, so here goes:
same :: a -> a -> Bool same x x = True same _ _ = False
Is there any way to do this in Haskell? The actual reason I'd like to do this is something slightly more complicated. I want to match against a list of elements, like so:
foo [x,x,x] = ...
If you want to check whether all elements of the list are the same, you'd need a function of type [a] -> Bool. There is no such function in the standard libraries (search for this type using Hoogle [1]). Fortunately, Hoogle will point us to all, which only needs a function from (a -> Bool) that is checked on every member of the list. One way we could solve this problem is to see whether all elements are equal to the first element. This is trivially expressed: allEqual :: Eq a => [a] -> Bool allEqual xs = all (== head xs) xs This also works for the empty list: because of lazy evaluation the expression "head xs" is only computed when actually needed. -chris [1]: http://www.haskell.org/hoogle/?q=[a]+-%3E+Bool
participants (4)
-
Alex Watt
-
Brandon S. Allbery KF8NH
-
Chris Eidhof
-
Tillmann Rendel