[erlang-questions] Basic question about Erlang binaries

Igor Ribeiro Sucupira igorrs@REDACTED
Sat Sep 26 22:43:31 CEST 2009


On Sat, Sep 26, 2009 at 5:04 PM, Robert Virding <rvirding@REDACTED> wrote:
> 2009/9/26 Igor Ribeiro Sucupira <igorrs@REDACTED>
>>
>> > This is not really strange if you consider what you are asking it to do.
>> > You
>> > are telling it to build a 1048575 bit integer,
>>
>> Am I?
>> Maybe I'm missing some concept, but to me it seems I'm asking it to
>> build Bit and to make sure the pattern matching is valid. So, what is
>> necessary is to check that the integer can be built and is unique.
>> Can't this be done in constant time?
>>
>> In other words, I believe the pattern matching above means something
>> like: "Can you please give me the 1048576th bit, as long as the first
>> 1048575 bits represent a unique (signed, little-endian, blablabla)
>> integer and and the rest represent an Erlang binary?"
>> I'm being pretty informal, as it's Saturday (:-)), but I believe you
>> understand my point.
>
> Yes I understand your point. :-)
>
> The problem is that what you MEAN is give me the 1048576th bit, but what you
> WRITE in your code is: take the first 1048575 bits and make an integer, then
> throw it away; take 1 bit and return it; put the remaining bits into a
> binary and then throw it away.
>
> This may be a bit on the philosophical side but pattern matching is really
> pulling a structure apart into its components and not really accessing a
> small part of a structure, although it is often used that way. As in this
> case. That is why you have to give a pattern for the whole binary. Or so I
> have always considered it.
>
> Now the interpreter is pretty naive and does exactly what you tell it, but
> the compiler should be better,


You are right. I experimented with some compiled code and it behaves
like a smart compiler should.  :-)

Thanks.
Igor.

-- 
"The secret of joy in work is contained in one word - excellence. To
know how to do something well is to enjoy it." - Pearl S. Buck.


More information about the erlang-questions mailing list