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?
You are using
do, whose result must be aMonadinstance, but your function’s type indicates that it returns aMapwhich is not an instance ofMonad. If you really want to returnNothingwhenle2 >= 1400, then you will have to returnJust ...whenle2 < 1400, and your function could look like this:Note that this function will return
Nothingif it can’t find the key in theMapto begin with…