Searching through list of tuples

后端 未结 3 394
别那么骄傲
别那么骄傲 2021-01-28 10:20

I\'m trying to write a function that accepts a string and a list of tuple pairs. I want to search through the list of tuples, and if the first value in the tuple matches the inp

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-28 10:40

    Just use a simple map combined with a lambda:

    searchMe :: (Eq a) => a -> [(a, b)] -> [Maybe b]
    searchMe a li = map (\(x, y) -> if a == x then Just y else Nothing ) li
    

    OP your function, as it is, won't work as you are defining your list parameter as (x : xs), which won't enable you to apply operations to elements of the tuple.

提交回复
热议问题