I'm trying to make a Haskell datatype a bit like a python dictionary, a ruby hash or a javascript object, in which a string is linked to a value, like so:
data Entry t = Entry String t
type Dictionary t = [Entry t]
The above code works fine. However, I would like a slightly nicer constructor, so I tried defining it like this:
data Entry t = String ~> t
This failed. I tried this:
data Entry t = [Char] ~> t
Again, it failed. I know that ~
has special meaning in Haskell, and GHCi still permits the operator ~>
, but I still tried one other way:
data Entry t = [Char] & t
And yet another failure due to parse error. I find this confusing because, for some inexplicable reason, this works:
data Entry t = String :> t
Does this mean that there are certain rules for what characters may occur in infix type constructors, or is it a cast of misinterpretation. I'm not a newbie in Haskell, and I'm aware that it would be more idiomatic to use the first constructor, but this one's stumping me, and it seems to be an important part of Haskell that I'm missing.
Dictionary t
will be nothing like "a Python dictionary, a Ruby hashmap", etc. It's just a linked list, nothing more. – WynnMaybe
. That's the easy part, but this I didn't understand. – ImeldaimelidaString
and anInt
in the sameDictionary
, lookup is going to be worst-case linear time, you can't destructively update (and you're never going to get the nicedict[key]
syntax). So it's not really like the datastructures you mention... – Partida