[erlang-questions] is there "return" in Erlang.
Edmond Begumisa
ebegumisa@REDACTED
Mon Feb 28 20:04:16 CET 2011
On Tue, 01 Mar 2011 05:01:53 +1100, Kostis Sagonas <kostis@REDACTED>
wrote:
> Edmond Begumisa wrote:
>> How about...
>> -define(CONF1, ..).
>> -define(CONF2, ..).
>> judge() ->
>> judge({a, get_a()}).
>> judge({a, ?CONF1}) ->
>> do_something(?CONF1);
>> judge({a, _}) ->
>> judge({b, get_b()});
>> judge({b, ?CONF2}) ->
>> do_other_thing(?CONF2);
>> judge({b, _}) ->
>> do_things().
>
> Now, why on earth would you ever want to do that? Wrap two terms in a
> tuple only to take them apart by pattern matching?
>
I interpreted the question to be how he could get a similar flow to the
original program rather than just literal conversion. I assumed his sample
code was a summerised snippet of a much larger C program where a
particular type of flow was giving him trouble duplicating in Erlang. I
immediately identified with his predicament.
Having been there before, I tried to illustrate how he could achieve this
by forgetting conversion and instead "adapting" to using tagging and
pattern matching. Above, a/b/c..z would be more meaningful tags in a
larger program (i.e. names of configuration parameters) Then he'd match
against particular parameters and perform the actions rather than a big
deep nested case/if statements. They'd likely be chunks of code between
where the tagging takes place and where it's matched so the tagging
wouldn't be wasteful.
> What's wrong with using an auxiliary judge function with two separate
> arguments instead?
>
> Or even better why not use separate judge_a and judge_b functions that
> are both more efficient and make the programmer's intention clearer?
>
Because that's not how his original program was flowing. His use of the C
"return" keyword indicated to me that the flow was important. That there
was a particular type of flow he couldn't figure out how to do, and *that*
was what he was interested in. Not the function itself.
> Sorry for the authoritative tone, but please think a bit more before you
> give "advice" to novices; they might actually take your advice literally
> and think that this is the proper way of writing programs in Erlang.
>
Again, my interpretation of the question was about controlling flow in
absence of a C-style return: How do I flow like this without a return
keyword? Adapting to think in terms of tagging and pattern-matching was
non-obvious and important for me when learning Erlang and took many weeks
of staring at OTP source to start to understand. I think the answer was
relevant to the question.
- Edmond -
> Kostis
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
More information about the erlang-questions
mailing list