[plt-scheme] mutable lists/dicts?
Thanks for the answers.
I know I can get what I want with vectors or hash tables, but I'm still trying to figure out the design decisions for mutable lists. If someone is used to association lists, then it seems natural (by extension) to use mutable association lists.
On Apr 10, 2010, at 10:05 AM, Matthew Flatt wrote:
> At Sat, 10 Apr 2010 09:47:09 -0400, Carl Eastlund wrote:
>> I'm not sure why mutable lists aren't dictionaries, though.
>
> Mutable association lists don't really fit the dictionary API:
> If you want to extend an empty mutable association list, then you need
> `dict-set!' to return a mutable pair, rather than void. Also, the empty
> mutable association list has the same representation as an association
> list.
Yes, that's indeed quite odd.
Why not having a separate representation (eg '{}) with the semantics being that you get a fresh empty mutable list?
> I suppose that mutable association lists could be supported if
> `dict-ref!' is allowed to update existing mappings but not add new
> ones, which would be like vectors. But what would be the point?
Well, there would still be at least one interesting difference, which is that you can lookup with an arbitrary non-number key.
Also, you could imagine that adding a new mapping be done using mappend!, no?
(agree that this would not make it possible to grow the empty mutable association list, but that's because this one is actually not mutable, as you point previously).
> (dict-set! '() 'x 10)
error: empty list is not mutable
> (define l (mlist (mlist 'a 1) (mlist 'b 2)))
{{a 1} {b 2}}
> (dict-set! l 'b 5)
> l
{{a 1} {b 5}}
> (dict-set! l 'c 3)
{{a 1} {b 5} {c 3}}
Thanks,
-- Éric