1

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 :)

3
  • 1
    Why do all your lists have only one element in them? Commented Nov 8, 2020 at 12:31
  • Well thats simply how my input data is, but it can be transformed anyway to achieve the end goal Commented Nov 8, 2020 at 12:37
  • Does this answer your question? Haskell. INNER JOIN of two lists Commented Nov 8, 2020 at 12:42

1 Answer 1

2

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"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.