With the below snippet I want to append/prepend a string to a list that is part of a value stored in a map.
From the snippet below I get the error
Couldn't match expected type `Char' with actual type `[Char]'
Expected type: Map.Map ([Char], Integer) [Char]
Actual type: Map.Map ([Char], Integer) [[Char]]
and I am not quite sure what that should tell me. Can that be solved with a change in the code or must there be something like
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
Together with an instance declaration?
import Data.Time
import Data.Time.Clock.POSIX
import qualified Data.PSQueue as PSQ
import qualified Data.Map as Map
import Data.Maybe
import Control.Category
import Control.Concurrent
import Control.Concurrent.MVar
import Control.Monad
key = ("192.168.1.1", 4711)
messages = ["aaa", "bbbb", "ccccc"]
newRq = do
time <- getPOSIXTime
let q = PSQ.singleton key time
let m = Map.singleton key messages
return (q, m)
appendMsg :: String -> (String, Integer) -> Map.Map ([Char], Integer) [Char] -> Map.Map ([Char], Integer) [Char]
appendMsg a (b, c) m = m2
where
f x = x ++ a
m2 = Map.adjust f (b, c) m
main :: IO()
main = do
(q, m) <- newRq
let m2 = appendMsg "first" key m
print (m2)
You need a change in the code. You start with a
Map (String, Integer) [String], by creating a singleton with valuemessages. Your update function however is written forMap (String, Integer) String. Either change it to the appropriate type by making itf x = x ++ [a](and change the type signature totoo) or change your initial map by using e.g.
unwords messages. (I think you’ll rather want the first.)