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

  • SEARCH
  • Home
  • 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 8774893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:40:46+00:00 2026-06-13T18:40:46+00:00

I have two functions: trans_table :: [Char] -> [Char] -> Map.Map Char Char trans_table

  • 0

I have two functions:

trans_table :: [Char] -> [Char] -> Map.Map Char Char
trans_table s1 s2 = Map.fromList (zip s1 s2)

random_trans_table :: IO (Map.Map Char Char)
random_trans_table = do
    rawKey <- shuffle ascii
    let zipped = zip ascii rawKey 
    let map = Map.fromList zipped
    return map    

First of them creates Map from two strings; second one generates random Map.
First one returns Map.Map Char Char; second one returns IO (Map.Map Char Char)

Now I need to lookup value from this Map and I have created two functions – one for IO Map and one for Map:

translate_char :: Map.Map Char Char -> Char -> Maybe Char
translate_char table c = Map.lookup c table

translate_char_io :: IO (Map.Map Char Char) -> Char -> IO (Maybe Char)
translate_char_io table c = do
    raw_table <- table
    let result = Map.lookup c raw_table
    return result

I don’t like it because it causes code duplication. I have already duplicate one function and if I code by this way I will need to duplicate all my functions.

Is there a way to create function which will works with Map and IO (Map) the same way ?

  • 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-13T18:40:47+00:00Added an answer on June 13, 2026 at 6:40 pm

    do-notation, or the “bind” operator >>= that it de-sugars to, takes care of this for you.

    Anywhere that you would have called translate_char_io, you can use a call to the pure function by letting the monad syntax unwrap your table for you. For example, if you want to create a random table and look up two different characters in it, you could do it this way:

    test c1 c2 = do
        table <- random_trans_table
        return (translate_char table c1, translate_char table c2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two functions that return simple strings. Both are registered. $.views.helpers({ parseDate: function
I have two functions function getImgStr(imgName){ thisStr = '<img src=/_shared/img/discovery/200x100/'+imgName+'.jpg>'; return thisStr; } function
I have two functions : void foo(const char * p) and template<size_t T_Size> void
Let's say for example that I have two functions with random code inside and
I have two functions here function Preloader() {} Preloader.prototype = { init:function() { //
I have two functions that caluclate and adjust the height and width of elements
I have two functions, but one of the functions is only called from the
I have two functions that have different enough logic but pretty much the same
I have two functions in an ActionScript class, they are: private function loaderCompleteHandler(event:Event):void {
I have two functions whose underlying logic is the same but in one case

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.