hashmap - Nested Map in Haskell -
what trying when have
rec1 = [("name", "obj-1"), ("status", "up"), ("region", "us")]
then have
[( "us", [("up", [("name", "obj-1"), ("status", "up"), ("region", "us")])])]
this code have come in haskell.
rec1 = [("name", "obj-1"), ("status", "up"), ("region", "us")] convertto rec = prelude.foldl (\map (k, v) -> data.map.insert k v map) data.map.empty rec justfold = prelude.foldr (\key val -> data.map.insert key val data.map.empty) (convertto rec1) ["us","up"]
in third line above, in lambda function definition, compiler not agreeing type of val. saying expects char , getting string or not able match map string list type.
i not haskell, looking if give me pointers it.
thanks.
edit: corrected output type. mistyped @ first.
there few issues code, easy, , hard. let's examine easy ones first:
the first convertto
function. exists already, , called fromlist
. there's function creating new map
1 pair in it, called singleton
. way you're referring functions in data.map
unusual. fixing these issues gives:
import qualified data.map m rec1 = m.fromlist [("name", "obj-1"), ("status", "up"), ("region", "us")] justfold = foldr m.singleton rec1 ["us","up"]
now however, there's deeper problem. figure out is, let's try adding type signatures rec1
, justfold
:
import qualified data.map m rec1 :: m.map [char] [char] rec1 = m.fromlist [("name", "obj-1"), ("status", "up"), ("region", "us")] justfold :: m.map [char] (m.map [char] (m.map [char] [char])) justfold = foldr m.singleton rec1 ["us","up"]
rec1
fine, justfold
problem. see why, @ type signatures of foldr
, singleton
:
foldr :: (a -> b -> b) -> b -> [a] -> b singleton :: k -> -> map k
foldr
requires function of type (a -> b -> b)
, , singleton
not that, because a
, map k a
not same thing. following code produces result want:
import qualified data.map m rec1 = m.fromlist [("name", "obj-1"), ("status", "up"), ("region", "us")] justfold = m.singleton "us" $ m.singleton "up" rec1
however, fundamental issue can't iterate on changes type on every iteration. if find wanting this, advise take step , rethink approach problem.
Comments
Post a Comment