[erlang-questions] Are recursive record definitions allowed?

James Aimonetti <>
Thu Jul 29 18:00:59 CEST 2010


I found Chris Okasaki's Purely Functional Data Structures to be
helpful in expanding my Erlang, data structures (duh), and functional
programming skills as well. Sometimes the conversion from imperative
to functional doesn't match up and this book helped bridge the gap a
bit. I should probably dust it off and go through it again :)

On Thu, Jul 29, 2010 at 5:22 AM, Brian Williams <> wrote:
> 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
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
>
>


More information about the erlang-questions mailing list