Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8881041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:16:06+00:00 2026-06-14T20:16:06+00:00

I have a question about Haskell. I want to know how I can create

  • 0

I have a question about Haskell. I want to know how I can create a list of data from two lists, one with data, an other with some key values. I’ll explain it with an example:

Given two lists: [('a', "red"), ('b', "blue"), ('c', "green")] and [('a','b'), ('b', 'c'), ('c','a')]. Now I want to change the values of the second list with their colors given in the first list. So the function should return [("red","blue"), ("blue","green"), ("blue","red")].

I was thinking about list comprehension, but I’m very new to Haskell and I have no idea how I should do that. Or is there an easier way to do this?

This is probably a dumb question, but if someone can give me an example, I might get used to the think process of Haskell a bit more.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:16:07+00:00Added an answer on June 14, 2026 at 8:16 pm
    -- second element from first (head) dropping tuples with bad key
    color :: Eq a => a -> [(a, b)] -> b
    color c = snd.head.dropWhile ((/=c).fst)
    
    recolor a b = map (\(x, y) -> (color x a, color y a)) b
    

    running

    Prelude> recolor [('a', "red"), ('b', "blue"), ('c', "green")] [('a','b'), ('b', 'c'), ('c','a')]
    [("red","blue"),("blue","green"),("green","red")]
    

    If you consider an element of the second list cannot be found in the first list. You can write

    color :: Eq b => b -> [(b, a)] -> Maybe a
    color c = fmap snd.listToMaybe.dropWhile ((/=c).fst)
    

    then

    Prelude> recolor [('a',"red"),('b',"blue"),('c',"green")] [('a','h'),('u','c'),('c','a')]
    [(Just "red",Nothing),(Nothing,Just "green"),(Just "green",Just "red")]
    

    (you need import Data.Maybe (listToMaybe))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about tuples and lists in Haskell. I know how to
I have a question about Haskell polymorphism. As I've learned, there are two types
I'm following a tutorial. (Real World Haskell) And I have one beginner question about
I have question about interpreting strings as packed binary data in C++. In python,
i have question about YAJLiOS parser... I have next json data : {{ body
I have a question about this formula from a book: EFW (cm,kg)= 10^(-1,7492+(0,166*BPD)+(0,046*AC)-(2,646*AC*BPD/1000)) The
again a total newbie question from me about Haskell, and Leskah. (First, a subjective
Reading Real world Haskell i found some intresting question about data types: This pattern
I have a beginner question about dates and String in Haskell. I need to
I have a question about the map function in Python. From what I understand,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.