浏览 1562 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-03-03
2008/7/19 Edwin Fine <emofine@gmail.com>: Litao, I think this is a bug in Erlang R12B-3. Certainly the documentation can be misleading, because in Programming Examples (Section 4.6: Matching Binaries), it specifically says that this construct is not allowed: "Size must be an integer literal, or a previously bound variable. Note that the following is not allowed: foo(N, <<X:N,T/binary>>) -> {X,T}. The two occurrences of N are not related. The compiler will complain that the N in the size field is unbound." That being said, if you rewrite the expression as shown below, it will compile (but does not work). If I understand correctly, binary, bits, and bitstream are the same, except that the default bit size for binary is 8, and for bitstring it is 1. Since you are overriding the default size anyway, you can use binary-unit:11 in place of bits-unit:11. decode(<<N:5,Chans:N/binary-unit:11,_/bits>>) -> [Chan || <<Chan:11>> <- Chans]. 103> Chans3 = <<3:5,2:11,3:11,4:11>>. <<24,2,0,96,4:6>> 104> bb:decode(Chans3). N:3, Chans:<<0,64,12,2,0:1>> ** exception error: no case clause matching {<<0,64,12,2,0:1>>} in function bb:'-decode/1-lc$^0/1-0-'/1 105> The code is: -module(bb). -compile([export_all]). decode(<<N:5,Chans:N/binary-unit:11,_/bits>>) -> io:format("N:~p, Chans:~p~n", [N, Chans]), [Chan || <<Chan:11>> <- Chans]. So even though the programming examples say that you can't use the same N in the match, it actually does work, but the list comprehension does not. I found out this is because a bitstring generator has to use "<=" and not "<-". So the final working code is: -module(bb). -compile([export_all]). decode(<<N:5,Chans:N/binary-unit:11,_/bits>>) -> [Chan || <<Chan:11>> <= Chans]. 118> c(bb). {ok,bb} 119> Chans3 = <<3:5,2:11,3:11,4:11>>. <<24,2,0,96,4:6>> 120> bb:decode(Chans3). [2,3,4] Hope this helps. 2008/7/18 litao cheng <litaocheng@gmail.com>: - Hide quoted text - hi, all. when I read this paper: Programming Efficiently with Binaries and Bit Strings http://www.erlang.se/euc/07/papers/1700Gustafsson.pdf, I encounter a compile error, the code snipes is a example to parse the IS 683-PRL protocol: decode(<<N:5,Chans:N/bits-unit:11,_/bits>>) -> [Chan || <<Chan:11>> <- Chans]. the compiler says: bit type mismatch (unit) between 11 and 1 I read the erlang reference mannual, the bits unit default is 1, I think the unit can be set, why this compile error occur? thank you! my erlang emulator is 5.6.3(R12B-3). _______________________________________________ erlang-questions mailing list erlang-questions@erlang.org http://www.erlang.org/mailman/listinfo/erlang-questions 难道他们不想修????我等只能凑合用。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-03-03
嘿嘿,委曲求全吧.呵呵. |
|
返回顶楼 | |
发表时间:2009-03-03
郁闷呀 2008/7/19 到现在 都半年多了呀
|
|
返回顶楼 | |