我写了以下类型的函数:元组评价
match :: [(String,a)] -> Maybe (String, a, a)
我希望函数遍历元组的列表,并确定是否有任何的元组中的第一个元素(一个字符串)是一样的。如果是这样,我想返回一个包含该字符串的元组,以及每个匹配元组中的第二个元素。如果没有匹配的元组,则返回“Nothing”。如果有多个匹配项,则返回找到的第一个匹配项。
例如:
match [("x", 3), ("y", 4), ("z", "5"), ("x", 6)] = ("x", 3, 6)
match [("x", 3), ("y", 4), ("z", "5")] = Nothing
我在想:
match (x:xs) = if (fst x) = (fst xs) return (fst x, snd x, snd xs)
--if no matches, return Nothing
感谢您的帮助!
我认为(“z”,“5”)是一种印刷错误,因为没有其他数字有引号。 –