[erlang-questions] Extensions to comprehensions eeps

Vlad Dumitrescu <>
Thu Jul 31 12:27:57 CEST 2008


On Thu, Jul 31, 2008 at 12:05, Hynek Vychodil <>wrote:

>
> On Thu, Jul 31, 2008 at 9:59 AM, Vlad Dumitrescu <>wrote:
>
>> 2008/7/31 Hynek Vychodil <>
>>
>>> There is functional difference, between <- and <= is not functional
>>> difference.
>>> There is not reason why
>>> [X || <<X>> <- <<"abc">> ].
>>> should not return same result as
>>>  > [X || <<X>> <= <<"abc">> ].
>>> "abc"
>>>
>>
>> Hi,
>>
>> I might be mistaken here, but I think the issue is that the compiler
>> generates completely different code for each case and when the generator
>> looks like X <- Y, i.e. the right side is not a constant, it has to know
>> which one to produce. The compiler can't guess from the source code, because
>> for example <<X>> <- L is a very normal list generator, one can't assume L
>> is a binary.
>>
>> best regards,
>> Vlad
>>
>>
> I understand it, but I think this is wrong design decision. Compiler
> optimization should not impact language design, especially in so clear
> functional language as Erlang is. {joke, "It should not evolve in ugliness
> of assembly language"}. For example, in some lisp compilers, I heard, are
> two entry point to function, with and without parameters check and so. And
> HiPE do some optimization for float only functions ... This is better way
> how deal with it. Changing language to solve some special low level
> optimizations is wrong idea.
>
> do_it(L) when is_list(L) -> [X+1, <<X>> <- L];
> do_it(B) when is_binary(B) -> [X+1, <<X>> <- B]. % [X+1, <<X>> <= B]
>
> should be same as
>
> do_it(Y) -> [X+1, <<X>> <- Y].
>
> and deal with it is compiler business.
>

Not quite. Compare these
7> [X || <<X,X>> <= <<1,2,2,3,4,4,5,6>>].
[4]
8> [X || [X,X] <- [1,2,2,3,4,4,5,6]].
[]
9> [X || [X,X] <- [[1,2],[2,3],[4,4],[5,6]]].
[4]

With binary comprehensions you are not limited to matching one element at a
time (like you are with lists), so there is a functional difference.

10> [{X,Y} || <<X,Y>> <= <<1,2,2,3,4,4,5,6>>].
[{1,2},{2,3},{4,4},{5,6}]

best regards,
Vlad
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080731/75d91070/attachment.html>


More information about the erlang-questions mailing list