[erlang-questions] orddict (was Re: OO programming style in Erlang?)

Richard A. O'Keefe <>
Fri Jan 26 03:15:15 CET 2007

"Ulf Wiger" <> wrote:
	I can think of one good reason to use ordsets/orddict: debugging.
	It is decidedly more enjoyable to watch (trace/debugging)
	printouts of ordsets and orddicts compared to sets/dicts.
	Pretty-printing of large dicts in the shell is in fact
	everything but pretty.
Way back in the very early 1980s, Chris Mellish was working on interpreting
English sentences (about elementary physics problems).  He quickly ran into
a debugging problem:  the semantic structures he was building were very
large, so the average procedure call as displayed by the debugger took
rather more than one screen-full.  So DEC-10 Prolog acquired the command
which was exactly like write(Term) but first checked for a user-defined
portray(Term), and called that instead if it was defined.  As I added
data structures to the DEC-10 Prolog library, it became clear that this
needed to be done recursively, so we arrived at the current definition.

print(Term) :-
    (   var(Term) -> write(Term)
    ;   number(Term) -> write(Term)
    ;   current_predicate(portray, portray(_)), portray(Term) -> true
    ;   like write/1 but recursive calls call print/1 instead of write/1

This meant that when testing a tree-like dictionary data structure,
it could be made to print as Dict{k1->v1, ..., kn->vn}, _whatever_ the
internal representation, as long as the principal functors (name/arity)
used were distinct from others.

It's a pity that Erlang's data representation has no way of labelling
terms like this.  If only records were a real data type, it _could_.

More information about the erlang-questions mailing list