1
votes

I am having difficulties understanding the issues with implementing certain functions. I have a general data type defined as follow :

import qualified Data.Map.Strict as Map
data Stuff x = Stuff (Map.Map x [x])

When I try to implement a function such as this :

foo :: Ord x => x -> x -> Stuff x -> Stuff x
foo a b (Stuff c) = Stuff (Map.insert (a b c))

I get an error:

Couldn't match expected type ‘Map.Map x [x]’
            with actual type ‘a0 -> Map.Map k0 a0 -> Map.Map k0 a0’

However, when I verify the signature in the Haskell docs:

insert :: Ord k => k -> a -> Map k a -> Map k a

If it's returning a "Map k a", can't type "a" be a list? How could I turn "a" into "[a]"?

1
Terminology note: [a] is the type of lists, not of arrays.duplode

1 Answers

8
votes

(a b c) applies function a to arguments b and c, when it looks like you want to provide a b and c as arguments to Map.insert. You can't provide b directly since it has the wrong type (x instead of [x]) but you can create a singleton list to insert:

foo :: Ord x => x -> x -> Stuff x -> Stuff x
foo a b (Stuff c) = Stuff (Map.insert a [b] c)