[erlang-questions] Inline If

Zoltan Lajos Kis kiszl@REDACTED
Sun May 9 01:20:19 CEST 2010


I guess the ternary operator only makes sense in non-functional 
languages, where conditional statements do not have return values.
Given that there was no ternary operator, you would have to duplicate 
the assignment part, rendering the code less readable:

if (a) { x = b; } else { x = c; };
/* x = a ? b : c; */

In a functional language, such as Erlang, the these statements do have a 
return value, so the ternary-operator would only be a syntactic sugar:

X = case A of true -> B; false -> C end.

Regarding your question, the answer depends on whether you need "lazy 
evaluation", and whether you like working with macros, or not.

Regards,
Zoltan.



On 5/9/2010 12:37 AM, Henning Diedrich wrote:
> Thanks Zoltan,
>
> so the answer would be
>
> -define(ASSERT(TEST,TRUE,FALSE), case TEST of true -> TRUE; false -> 
> FALSE end).
>
> This seems to imply that even just wanting to use such construct is 
> not a common thing in Erlang.
>
> Is there a reason for that in the nature of FP or Erlang?
>
> Thanks,
> Henning
>
>
> Zoltan Lajos Kis wrote:
>> On 5/9/2010 12:00 AM, Henning Diedrich wrote:
>>> Is there a common Erlang way to do stuff like C++:
>>>
>>>    a ? b : c;
>>>
>>> The list comprehension
>>>
>>>    [X || X <- [B,C], A ]
>>>
>>> ?
>>>
>>> Thanks,
>>> Henning
>>>
>>
>> You can define a function or macro for this purpose; see "assert" 
>> discussed in this thread: Ternary operator used as assert
>> http://www.erlang.org/cgi-bin/ezmlm-cgi?4:sss:48575:200912:jchpnnbpampjdbbfpigm 
>>
>>
>> Regards,
>> Zoltan.
>>
>



More information about the erlang-questions mailing list