Reputation: 39
I have a list of lists in single chars like: [["a"],["b"],["c"],["d"]]
,
and I have a map for example [("a", "A"), ("b", "B")]
, I would like to find elements in list that match the map keys and replace the list value with the map value for that key and remove all of the remaining unchanged single chars.
So for example from the above, if I have list of [["a"],["b"],["c"],["d"]]
and map of [("a", "A"), ("b", "B")]
I want to get back a single list like this: ["A", "B"]
As I am a total noob with Haskell so any help will be appreciated :)
Upvotes: 1
Views: 664
Reputation: 35540
You can combine lookup
with catMaybes
:
import Data.Maybe
list :: [[String]]
list = [["a"],["b"],["c"],["d"]]
replacements :: [(String, String)]
replacements = [("a", "A"), ("b", "B")]
replaced :: [String]
replaced = catMaybes . map (\x -> lookup x replacements) . concat $ list
main :: IO ()
main = print replaced -- ["A", "B"]
Upvotes: 2