[erlang-questions] is there "return" in Erlang.

Attila Rajmund Nohl attila.r.nohl@REDACTED
Mon Feb 28 15:57:55 CET 2011


2011/2/28, Masklinn <masklinn@REDACTED>:
>
> On 2011-02-28, at 14:59 , Attila Rajmund Nohl wrote:
>
>> 2011/2/28, Jachym Holecek <freza@REDACTED>:
>> [...]
>>> And about return in general -- in Erlang (and other functional languages)
>>> program is composed of expressions that get evaluated to values, there
>>> are
>>> no statements. This is the reason why you do things like:
>>>
>>>   A = case (X rem 2) of
>>> 	   0 ->
>>> 	       even;
>>> 	   1 ->
>>> 	       odd
>>>       end
>>>
>>> which quite annoyingly you can't do in C.
>>
>> I might misunderstand something, but
>>
>> A = (X % 2 == 0) ? even : odd;
>>
>> can be done in C …
> Yes, his case was overly simplistic and thus easy to reproduce with a
> ternary. But a ternary is not going to scale very far (not without making
> the next person having to maintain your code go postal) whereas a case can
> grow quite a bit (in number of patterns, for instance)

That's where "else if" comes into play in imperative languages. It
might not be that elegant if the variable has to get a value in each
branch, on the other hand it might be possible that there's no real
need for that variable to get a value...

Don't forget that "Erlang is a functional language" is a nice fairy
tale, but in reality Erlang is used to control stuff or communicate
with the outside world, so the side-effects are very important and
many case clauses contain only an 'ok' atom (because something had to
be returned, even if nobody cares). Real-world example:

update_index_next(measTable, Reply, NewCols, RowIndex) ->
    maybe_start_measurement(NewCols),
    update_index_next(?measRowStatus, ?indexKey, Reply, NewCols, RowIndex).

maybe_start_measurement(NewCols) ->
    case {get_value_from_column_list(?measRowStatus, NewCols),
	  get_value_from_column_list(?measAdminStatus, NewCols)} of
	{?measRowStatus_active, ?measAdminStatus_up} ->
	    really_start_measurement(NewCols);
	_ ->
	    dont_start_measurement
    end.

Obviously only the side-effect is important, still, two totally
meaningless line had to be written.


More information about the erlang-questions mailing list