[erlang-questions] Are recursive record definitions allowed?

黃耀賢 (Yau-Hsien Huang) g9414002.pccu.edu.tw@REDACTED
Wed Jul 28 19:15:30 CEST 2010


On Wed, Jul 28, 2010 at 3:28 AM, Brian Williams <mixolyde@REDACTED> wrote:

> I'm working on some algorithm analysis and am trying to define a
> fairly simple binary search tree with a record definition:
> -record(btreenode, {left = #btreenode{}, right = #btreenode{}, value}).
> I get:
> record btreenode undefined
>
> Should I just not bother to specify the type of left and right and
> just enforce the record type in code?
>

Although it's comprehensible in Haskell, I'd never seen any tree
definition in Erlang in this style. The `record' syntax is not to
define types with a type constructor and its data constructors.
It's just a record with fields about some data.

Thus, to define the tree structure, it is like

    -record(btree, {value, ltree, rtree}).

And fill data into those field: for example, a blind tree

    tree([]) ->
        nil;
    tree([X|Xs]) ->
        T = tree(Xs),
        #btree{value=X,ltree=T,rtree=T}.

> test:tree([1,2,3]).
{btree,1,
       {btree,2,{btree,3,nil,nil},{btree,3,nil,nil}},
       {btree,2,{btree,3,nil,nil},{btree,3,nil,nil}}}


More information about the erlang-questions mailing list