
HI Cafe I have to make a function to check that I have one occurrence of the last element (z) of the same list [a,b] in the tuple [([a,b],z)] For example [([1,2],3),([1,1],5),([1,3],6).......] this is true because there is one single z for each single list. while this one is false [([1,2],3),([1,2],5),([1,3],6).......] because 3&5 were found for the same list [1,2] any Idea how to code this Fn. Thanks

On Sun, Apr 10, 2011 at 12:49 PM, Anwar Bari
HI Cafe I have to make a function to check that I have one occurrence of the last element (z) of the same list [a,b] in the tuple
[([a,b],z)] For example [([1,2],3),([1,1],5),([1,3],6).......] this is true because there is one single z for each single list.
while this one is false [([1,2],3),([1,2],5),([1,3],6).......] because 3&5 were found for the same list [1,2]
any Idea how to code this Fn. Thanks
This seems to work... import Data.List a = [([1,2],3),([1,1],5),([1,3],6)] b = [([1,2],3),([1,2],5),([1,3],6)] test :: Ord a => [([a], a)] -> Bool test = not . hasDuplicates . sort where hasDuplicates [] = False hasDuplicates [_] = False hasDuplicates (x:xs) = fst x == fst (head xs) || hasDuplicates xs main = do print $ test a print $ test b Rob

On Sun, 10 Apr 2011 18:49:59 +0200, Anwar Bari
HI Cafe I have to make a function to check that I have one occurrence of the last element (z) of the same list [a,b] in the tuple
[([a,b],z)] For example [([1,2],3),([1,1],5),([1,3],6).......] this is true because there is one single z for each single list.
while this one is false [([1,2],3),([1,2],5),([1,3],6).......] because 3&5 were found for the same list [1,2]
import Data.Function (on) import Data.List (groupBy, nub, sort) a = [([1,2],3),([1,1],5),([1,3],6)] b = [([1,2],3),([1,2],5),([1,3],6)] check = all ((< 2) . length . nub) . groupBy ((==) `on` fst) . sort main = print $ check a && not (check b) I don't know if I interpreted your specifications correctly; this program ignores identical tuples, so check [([1,2],3), ([1,2],3), ([1,3],6)] returns True; if this is not what you meant, remove the " . nub" from the program. Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --

On 11/04/2011, at 4:49 AM, Anwar Bari wrote:
HI Cafe I have to make a function to check that I have one occurrence of the last element (z) of the same list [a,b] in the tuple
[([a,b],z)] For example [([1,2],3),([1,1],5),([1,3],6).......] this is true because there is one single z for each single list.
while this one is false [([1,2],3),([1,2],5),([1,3],6).......] because 3&5 were found for the same list [1,2]
any Idea how to code this Fn.
Let m = length xs has_duplicate_key [] = False has_duplicate_key ((k,v):xs) = if null [v' | (k',v') <- xs, k' == k] then has_duplicate_key xs else True is perhaps the obvious code, but it's O(n**2). Sorting on the first element of the pairs takes O(n.lg n) time, and then checking for two adjacent (k,v),(k,v') pairs takes O(n). You can also use Data.Map in a fairly obvious way.
participants (5)
-
Anwar Bari
-
Henk-Jan van Tuyl
-
Henning Thielemann
-
Richard O'Keefe
-
Rob Nikander