Records, encapsulation and optional elements
Wed May 22 13:14:48 CEST 2002
Alex Peake wrote:
> Thank you so much. Much more elegant!
> (I also got useful suggestions from Ingela Anderton - similar to your second
> solution - and Willem Broekema).
Well my solution was of the top of my head. The first thing that
usually comes to mind when programing a functional language as Erlang
is recursion. Although some times it can be beneficial to use higher
order functions from the lists library and not do all the recursion
explicit. The list functions are very powerful but might
in some cases make the code harder to understand. I guess it is a bit
a matter of taste and I always decide from case to case which I prefer.
I said in may mail that I would suggest something like
that because I wanted to show the principal but I really had not put a
lot of effort into the example. I changed your program as little as
possible, any I even managed to put a bug into it. (I guess it always ends
up being more work when you are lazy ;)) Well which ever
way you decide Willems code was a cleaner example.
> It seems, then, that there is a trade-off between the compact, elegant code
> solutions that you suggest and my clumsy attempt:
> * Your solutions involve copying the record several times before completion
> * My solution looks ugly (and creates lots of intermediate variables)
> Is that a reasonable assessment?
> BTW, I assume the copying cost is not significant?
Well Ulf Wiger already answered most of this. I just wanted to add
that your first attempt reminded me more of a C program than an Erlang
program. When programing Erlang you should think pattern matching and
recursion. Also you should first make things work and then make them
faster if necessary. Especially if you want to use dirty tricks. Most
of the time however dirty tricks are unnecessary and
it will be sufficient to use the data structures and mechanisms in a
wise way. If you are intrested in avoiding common misuses that make
things more inefficient you should take a look at:
//The highway of life is always under construction. //
|\ _,,,--,,_ ,)
/,`.-'`' -, ;-;;'
|,4- ) )-,_ ) /\
Ericsson AB - OTP team
Cellular/Mobile: +46 70 636 78 68
More information about the erlang-questions