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 7497757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:13:07+00:00 2026-05-29T19:13:07+00:00

I have tried this in multiple ways: if-then-else, guards, case statements but I never

  • 0

I have tried this in multiple ways: if-then-else, guards, case statements but I never manage to get it compiled. I think from the code it is quite clear what I want to do. Why is this not possible and what must I do to get it straight?

appendMsg :: String -> (String, Integer) -> Map.Map (String, Integer) [String] -> Map.Map (String, Integer) [String]
appendMsg  a (b,c) m = do
      let Just le1 = length . concat <$> Map.lookup (b,c) m
          le2 = le1 + length a
      if le2 < 1400 then (let m2 = Map.adjust (x ++ [a]) (b, c) m) else (print (le1, le2))
      return (m2)

The error message I get here is parse error on input `)'. If I change the brackets I get parse error on input `else'. If I say in the else path (let m2 = m) then I get again the bracket error.

What I try to achieve is, if le2 <= 1400 then m2 should be created by using

f x = x ++ [a]
m2 = Map.adjust f (b, c) m

if le2 > 1400 then an other function should be called that takes the same arguments than appendMsg and appendMsg should simply return nothing.

An additional problem is that as soon as this is set right according to all recommendations below:

if le2 < 1400 then let m2 = Map.adjust (++ [a]) (b, c) m in return (m2) else return ()

I get an error saying

 No instance for (Monad (Map.Map (String, Integer)))
      arising from a use of `return'

That is by the way the same error I eventually got when I used guards. The problem is that there is little I can do in the remaining code with this strange type. Can I convert this type then in any way back to (Map.Map (String, Integer) [String]) or avoid it altogether?

  • 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-05-29T19:13:10+00:00Added an answer on May 29, 2026 at 7:13 pm

    You are using do, whose result must be a Monad instance, but your function’s type indicates that it returns a Map which is not an instance of Monad. If you really want to return Nothing when le2 >= 1400, then you will have to return Just ... when le2 < 1400, and your function could look like this:

    import qualified Data.Map as Map
    import Control.Monad
    
    appendMsg :: String -> (String, Integer) ->
                 Map.Map (String, Integer) [String] ->
                 Maybe (Map.Map (String, Integer) [String])
    appendMsg  a k m = do
          v <- Map.lookup k m
          let l = length . concat $ v 
              l' = l + length a
          guard $ l' < 1400
          return $ Map.insert k (v ++ a) m
    

    Note that this function will return Nothing if it can’t find the key in the Map to begin with…

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

Sidebar

Related Questions

I am feeling stupid but have tried multiple ways of having new line in
Im getting an error using this code below. I have tried multiple ways and
There appears to be multiple ways to do this, but the examples I've tried
I have tried this but it does not work (even if I specify .wav
i have tried this code to redirect a php page.but it s not working
I have tried this but it doesn't work: tell application Preview set myfile to
I have tried this a few different ways and it always seems to fail.
I've tried this a couple ways, the first is have a class that implements
I understand there are MANY posts on this, but I have tried every single
I've tried multiple ways to block a folder of logs from Mercurial but it

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.