[erlang-questions] confused by the grammar of split_binary/2 in the example of mp3_sync

Hynek Vychodil <>
Sun Oct 12 18:55:20 CEST 2008


2008/10/12 lang qiu <>

> Hi,
>
> I am confused by the grammar of split_binary/2 in the example of mp3_sync
> in the book "Programming Erlang",
>
> get_word(N, Bin) ->
>    * {_,<<C:4/binary,_/binary>>} = split_binary(Bin, N), C.*
>
> I understand that *@spec split_binary(Bin, Pos)* -> {Bin1, Bin2} but how
> does the above clause work ? How does it make C get the first 4 bytes of Bin
> starting from N ?
>

get_word(N, Bin) ->
   * {_,<<C:4/binary,_/binary>>} = split_binary(Bin, N),
    C.

*is shorthand for:*

*get_word(N, Bin) ->
   * {Bin1, Bin2} = split_binary(Bin, N),
    **<<C:4/binary,_/binary>> = Bin2,*
*     C.*

Another way how to write it:

get_word(N, Bin) ->
   *<<_:N/binary, C:4/binary,_/binary>> = Bin,*
*    C.*


>
> Thanks!
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



-- 
--Hynek (Pichi) Vychodil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081012/f81dd7c0/attachment.html>


More information about the erlang-questions mailing list