I would like to use multiple times Eq,
so that the second element of the first tuplet is from another type than the rest
Wrong, but, it is the idea what I want
eg. [(a, a)] -> [(a, a)] -> Bool —-> [(a, b)] -> [(a, a)] ->
Bool
the code
canColor :: Eq a => [(a, a)] -> [(a, a)] -> Bool
canColor _ [] = True
canColor xs ((x,y):rest) =
if findNeighbour xs x == findNeighbour xs y
then False
else canColor xs rest
findNeighbour :: Eq a => [(a, a)] -> a -> Maybe a
findNeighbour [] _ = Nothing
findNeighbour ((x,y):rest) z =
if x == z
then Just y
else findNeighbour rest z
The inputdata and expectation value
Main> canColor [('a',"purple"),('b',"green"),('c',"blue")] [('a','b'),('b','c'),('c','a')]
True
Main> canColor [('a',"purple"),('b',"green"),('c',"purple")] [('a','b'),('b','c'),('c','a')]
False
Main> canColor [('1',"purple"),('2',"green"),('3',"blue")] [('1','2'),('2','3'),('3','1')]
True
**Main> canColor [('a', 4),('b',5),('c', 6 )] [('a','b'),('b','c'),('c','a')]
True
Main> colors [('a', 4),('b', 4 ),('c', 5 )] [('a','b'),('b','c'),('c','a')]
False**
Simply give them different type variables and require
Eqfor both. I think you are looking for this code:canColor :: (Eq a, Eq b) => [(a, b)] -> [(a, a)] -> Bool canColor _ [] = True canColor xs ((x,y):rest) = if findNeighbour xs x == findNeighbour xs y then False else canColor xs rest findNeighbour :: Eq a => [(a, b)] -> a -> Maybe b findNeighbour [] _ = Nothing findNeighbour ((x,y):rest) z = if x == z then Just y else findNeighbour rest zor this more succinct and idiomatic code: