[erlang-questions] Are recursive record definitions allowed?

Brian Williams <>
Thu Jul 29 14:22:10 CEST 2010


This is essentially what I'm doing, but using the undefined atom. Leaf
technically is a node that has a value, but no children. it's not
really the name of an empty left or right node pointer. Here, new_node
generates a leaf.

new_node(Value) ->
    #btreenode{left = undefined, right = undefined, value = Value}.

insert([], Tree) -> Tree;
insert([Head | Tail], Tree) -> insert(Tail, insert(Head, Tree));
insert(Value, undefined) -> new_node(Value);
insert(Value, Tree = #btreenode{value = Value}) -> Tree;
insert(Value, #btreenode{left = Left, right = Right, value = CurrentValue})
    when Value < CurrentValue ->
        #btreenode{left = insert(Value, Left), right = Right, value =
CurrentValue};
insert(Value, #btreenode{left = Left, right = Right, value = CurrentValue})
    when Value > CurrentValue ->
        #btreenode{left = Left, right = insert(Value, Right), value =
CurrentValue}.

Writing somre more complicated operations and homework assignments
next. I have found rewriting old homework assignments from Algorithms
and AI classes to be very helpful in teaching myself the language.

On Thu, Jul 29, 2010 at 7:17 AM, Fredrik Andersson <> wrote:
> On Wed, Jul 28, 2010 at 7:39 PM, Tony Arcieri <> wrote:
>> On Tue, Jul 27, 2010 at 1:28 PM, Brian Williams <> 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}).
>>
>>
>> Wouldn't you want the defaults to be for the leaf nodes, and if so,
>> shouldn't they be some sort of nil/null/void atom to indicate there are no
>> left/right branches?
>>
>> --
>> Tony Arcieri
>> Medioh! A Kudelski Brand
>>
>
> Why not even make them the atom leaf? :)
>



-- 
Brian E. Williams

http://www.techhouse.us/wordpress-mu/brianw

"Never attribute to malice that which can be adequately
explained by stupidity." - Hanlon's Razor


More information about the erlang-questions mailing list