[erlang-questions] Why does Erlang have control structures?

Toby Thain <>
Wed Aug 29 01:36:14 CEST 2012


On 28/08/12 7:04 PM, Volodymyr Kyrychenko wrote:
> Jayson Barley wrote:
>> I am not sure I understand why we have them. For instance I can take the
>> following code
>>
>> is_greater_than(X, Y) ->
>>      if
>>          X>Y ->
>>              true;
>>          true ->  % works as an 'else' branch
>>              false
>>      end.
>>
>> And make it
>>
>> is_true(true) ->
>>      true;
>> is_true(false) ->
>>      false.
>>
>> is_greater_than(X, Y) ->
>>      is_true(X>Y).
>
> Because erlang has no call-by-name/need.
>
> What you're proposing exists in Smalltalk.
>
> x ifTrue: [ code ]

This lazy block passing also exists in Scala, which enables various 
forms of ad-hoc control structures like:

   spawn {
     // code block to run in another thread
   }

or

   val f = future { /* some computation wanted later */ }

Other Scala features of interest to Erlangers are immutable bindings, 
pattern matching, and of course actors.

--Toby

>
> In erlang for this to work it should be like:
>
> if_(X>Y, fun() ->  do something end, fun() ->
> this_is_else_for_something_to_return_if_not end).
>
> For this to work without having to wrap everything into funs there
> should be lazy evaluation order in the language.
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list