[erlang-questions] gb_trees, strange behaviour
Robert Virding
robert.virding@REDACTED
Fri Nov 5 21:44:59 CET 2010
The reason is that orddict is defined to have exactly the same interface as dict. So it has a to_list/1. In dict this actually builds a new list while orddict just returns its list, of course.
Robert
----- "Michal D." <michal.dobrogost@REDACTED> wrote:
> > Michal D. wrote:
> >>
> >> Hi again,
> >>
> >> I'm having a lot of problems starting out with Erlang. It appears
> to me
> >> that
> >> I can't update a specific key of a gb_tree, while the other ones
> work
> >> fine.
> >> Can anyone explain the following behaviour?
> >>
> >>> X =
> gb_trees:from_orddict([{trace,false},{limit,-1},{timeout,-1}]).
> >>
> >> {3,{limit,-1,{trace,false,nil,nil},{timeout,-1,nil,nil}}}
> >>>
> >>> gb_trees:update(limit,true,X).
> >>
> >> {3,{limit,true,{trace,false,nil,nil},{timeout,-1,nil,nil}}}
> >>>
> >>> gb_trees:update(timeout,true,X).
> >>
> >> {3,{limit,-1,{trace,false,nil,nil},{timeout,true,nil,nil}}}
> >>>
> >>> gb_trees:update(trace,true,X).
> >>
> >> ** exception error: no function clause matching
> >> gb_trees:update_1(trace,true,nil)
> >> in function gb_trees:update_1/3
> >> in call from gb_trees:update/3
> >
> > The update calls that you think they work, work by fluke. The
> problem is
> > that even though you may think you have created a gb tree, but in
> fact you
> > have not.
> >
> > The problem lies in your first call:
> >
> > X =
> gb_trees:from_orddict([{trace,false},{limit,-1},{timeout,-1}]).
> >
> > You promised you will supply an orddict, but in fact you have not.
> > Use:
> >
> > X =
> >
> gb_trees:from_orddict(orddict:from_list([{trace,false},{limit,-1},{timeout,-1}])).
> >
> > instead and things will work as expected.
> >
> > Kostis
> >
>
> Thank you for the analysis!
>
> Maybe we should update the documentation? It's pretty common to use a
> list of tuples as a dictionary in other languages and that's what
> appears to be specified in the Erlang documentation:
>
> ----------
> from_orddict(List) -> Tree
>
> Types:
>
> List = [{Key, Val}]
> Key = Val = term()
> Tree = gb_tree()
>
> Turns an ordered list List of key-value tuples into a tree. The list
> must not contain duplicate keys.
> ----------
>
> Although on a second reading, it seems that the word "orderered" in
> "ordered list" is key. So is an orddict just an ordered list of
> tuples? But then again, why would it have to_list/1 if it was just a
> list?
>
> Cheers,
>
> Michal
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
More information about the erlang-questions
mailing list